PHP email - From header

If you do not want to have your PHP Email to show as being sent from the cPanel user, the following options will allow you to change that.

These instructions are provided for your convenience, but please remember we do not offer support to code this, nor do we promise to make your code work. A web developer or support group online may be a better resource for coding assistance.


Change PHP Email "From" Header

Your PHP mail() script emails always show "From: cPanel-username@servername." You can change that by adding the following code:

$headers = 'From: user@example.com' . " " .
'Reply-To: user@example.com' . " " .
'X-Mailer: PHP/' . phpversion();

Then, just add $headers to the mail function. For additional information, please review the following article:

Specified Header Still Not Being Displayed?

If mail is still being sent as the cPanel user and not the user specified in the header variable, using the -f parameter will correctly set the from address.
You can do this by adding the following code:

mail('user@hisdomain.com', 'TEST', 'TEST', null, '-fuser@example.com'); 
?>

After adding the above -f parameter, the final script code would look like:

$headers = 'From: user@example.com' . " " .
'Reply-To: user@example.com' . " " .
'X-Mailer: PHP/' . phpversion();

mail('user@example.com', 'TEST', 'TEST', null, '-fuser@example.com');

?>

Note:  You must add the header variable as the 4th attribute in the mail() function. EXAMPLE - mail($to, $subject, $message, $headers);

Add Cc and Bcc Headers

You can add Cc and Bcc headers to your emails by adding this code:

$headers = 'From: user@example.com' . " " .
'Cc: user@hisdomain.com' . " " .
'Bcc: user@herdomain.com' . " " .
'Reply-To: user@example.com' . " " .
'X-Mailer: PHP/' . phpversion();