"From" Header Issue in PHP Mail
In an attempt to enhance user functionality, a website was developed to automatically send emails upon user registration. The PHP code employed for this task is detailed below:
<?php $to = "[email protected]"; $subject = "Test mail"; $message = "Hello! \nThis is a simple email message."; $headers = "From: [email protected]"; $headers .= "\r\nReply-To: [email protected]"; $headers .= "\r\nX-Mailer: PHP/".phpversion(); mail($to,$subject,$message,$headers); echo "Mail Sent."; ?>
Upon sending emails through this code, an unexpected issue arose. The "From" header displayed in the delivered emails remained as "[email protected]," while the "Reply-To" header correctly reflected the specified "[email protected]" value. The mail server used was box123.bluehost.com.
Cause of the Issue
After further investigation, it was determined that the use of a Gmail address as the "From" value was the root cause of the problem. Internet Service Providers (ISPs), including Bluehost, commonly restrict the use of external email addresses as the "From" value to prevent email spoofing. As a result, the ISP overwrote the "From" address with its default value.
Workaround
To address this issue and redirect replies to the intended address, it is recommended to use the "Reply-To" header instead. Additionally, a fifth parameter can be added to the mail() function as shown below:
mail($to,$subject,$message,$headers,"-f [email protected]");
This parameter specifies the "-f" option, which allows manual setting of the "From" header for many ISPs.
The above is the detailed content of Why is my PHP mail() function\'s \'From\' header being ignored, and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!