Home > Backend Development > PHP Tutorial > Why is my PHP mail() function\'s \'From\' header being ignored, and how can I fix it?

Why is my PHP mail() function\'s \'From\' header being ignored, and how can I fix it?

DDD
Release: 2024-12-01 10:54:09
Original
543 people have browsed it

Why is my PHP mail() function's

"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&#160;protected]";
$subject = "Test mail";
$message = "Hello! \nThis is a simple email message.";

$headers = "From: [email&#160;protected]";
$headers .= "\r\nReply-To: [email&#160;protected]";
$headers .= "\r\nX-Mailer: PHP/".phpversion();

mail($to,$subject,$message,$headers);

echo "Mail Sent.";
?>
Copy after login

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&#160;protected]");
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template