Change the From: email address in PHP mail()

Q. How do I change the email address in PHP mail() function?

A. The php mail() function allows you to send email.

mail() syntax:
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )

=> to: Receiver, or receivers of the mail.

=> subject: Subject of the email to be sent.

=> message: Message to be sent.

=> additional_headers: String to be inserted at the end of the email header.
This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (rn).

string additional_headers is use to specify From: email address in PHP. Generally you write mail() as follows:

$message = "This is a test";
mail('[email protected]', 'Subject', $message);

If you would like to add From : email address

Use code as follows:

$to      = '[email protected]';
$subject = 'Subject';
$message = 'This is a test';
$headers = 'From: [email protected]' . "rn" .
   'Reply-To: [email protected]' . "rn" .
   'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);


Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.