When using PHP's mail() function, why is the "From" address wrong?

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.

  • 106 Users Found This Useful
Was this answer helpful?

Related Articles

Do you support Ioncube?

Yes, we have compiled our servers with Ioncube support prebuilt, so no configuration on your part...

Do you support both PHP4 and PHP5?

We provide many different versions of PHP to our end users. If the version currently installed on...

Why do I get a PHP open_basedir restriction error message?

This error generally occurs when you are using temporary site links whilst waiting for DNS...

Do you support the MySQLi PHP extension?

The MySQLi PHP extension is installed and enabled on all of our shared and semi-dedicated servers.