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:

username@server.hostname

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 = "recipient@domain.tld";
$subject = "Testing From Address";
$message = "This is a test, please disregard.";
$headers .= 'Return-Path: test@domain.tld' ."\r\n";
$headers .= 'From: Sender <test@domain.tld>' . "\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, "-femail.address@example.com");

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

"From username@server.hostname on behalf of test@domain.tld"

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.

  • 151 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.