Knowledgebase : PHP
When using PHP's mail() function, why is the "From" address wrong?
Posted by Administrator on 17 September 2018 03:25 am

By default, PHP will send e-mail from the account you are running the script from. Usually this is in the format of:

[email protected]

To ensure the From: address displays correctly to your users, please ensure you are sending the return-path, reply-to and from headers with the mail() function. More information can be found from the PHP website. The following code has been tested to work successfully on our shared servers. Please be advised we do NOT support this code, and we will not help you with development of your mail scripts. The code shown is merely an illustration of correct header configuration:

$to = "[email protected]";
$subject = "Testing From Address";
$message = "This is a test, please disregard.";
$headers .= 'Return-Path: [email protected]' ."\r\n";
$headers .= 'From: Sender <[email protected]>' . "\r\n";
mail($to, $subject, $message, $headers);

In addition to the above, to "fix" the return-path header you will also need to invoke the mail() function with an extra "-f" parameter as such:

mail($to, $subject, $message, $headers, "[email protected]");

Please note that if using Microsoft Outlook, or Hotmail (Live), you will see something similar to:

"From [email protected] on behalf of [email protected]"

This is normal behaviour, and unavoidable due to the way our servers add the Sender header to all e-mails for abuse tracking purposes. Other clients and companies (such as Google Apps) utilise the From header for sender sourcing, so do not exhibit this behaviour.