How to control the incoming mails details in php
In PHP, you can control the incoming mail details using various functions and settings. Here are some ways to do so:
mail()
function: Themail()
function is used to send an email. You can specify the sender's email address, recipient's email address, subject, and message body. For example:mail('[email protected]', 'Subject', 'Hello, this is a test email');
headers
parameter: You can specify additional headers using theheaders
parameter of themail()
function. For example:mail('[email protected]', 'Subject', 'Hello, this is a test email', 'From: [email protected]');
From
header: You can set theFrom
header using theFrom
parameter of themail()
function. For example:mail('[email protected]', 'Subject', 'Hello, this is a test email', 'From: [email protected]');
Reply-To
header: You can set theReply-To
header using theReply-To
parameter of themail()
function. For example:mail('[email protected]', 'Subject', 'Hello, this is a test email', 'Reply-To: [email protected]');
CC
andBCC
headers: You can set theCC
andBCC
headers using theCC
andBCC
parameters of themail()
function. For example:mail('[email protected]', 'Subject', 'Hello, this is a test email', 'CC: [email protected]', 'BCC: [email protected]');
Content-Type
header: You can set theContent-Type
header using theContent-Type
parameter of themail()
function. For example:mail('[email protected]', 'Subject', 'Hello, this is a test email', 'Content-Type: text/html');
boundary
parameter: You can set theboundary
parameter of themail()
function to specify a boundary for multipart emails. For example:mail('[email protected]', 'Subject', 'Hello, this is a test email', 'boundary="----boundary----"');
PHPMailer
library: You can use the PHPMailer library to send emails with more advanced features, such as attachments, HTML emails, and more. For example:require_once 'PHPMailer/PHPMailer.php'; $mail = new PHPMailer(); $mail->setFrom('[email protected]'); $mail->addAddress('[email protected]'); $mail->Subject = 'Subject'; $mail->Body = 'Hello, this is a test email'; $mail->send();
SwiftMailer
library: You can use the SwiftMailer library to send emails with more advanced features, such as attachments, HTML emails, and more. For example:require_once 'SwiftMailer/Swift.php'; $mail = new Swift_Mailer(); $mail->setFrom('[email protected]'); $mail->addAddress('[email protected]'); $mail->Subject = 'Subject'; $mail->Body = 'Hello, this is a test email'; $mail->send();
filter_var()
function: You can use thefilter_var()
function to validate and sanitize the email address before sending the email. For example:$email = '[email protected]'; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo 'Invalid email address'; } else { mail($email, 'Subject', 'Hello, this is a test email'); }
These are some of the ways you can control the incoming mail details in PHP.