problem with php mail 'From' header

2019-01-01 11:32发布

I'm building a website that sends and email to a user when he registers.

My code (the gist of it):

<?php
$to = "helloworld@gmail.com";
$subject = "Test mail";
$message = "Hello! \nThis is a simple email message.";

$headers = "From: munged@gmail.com";
$headers .= "\r\nReply-To: munged@gmail.com";
$headers .= "\r\nX-Mailer: PHP/".phpversion();

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

echo "Mail Sent.";
?> 

the problem is that when the mail is delivered, the from header remains munged@box123.bluehost.com, while reply-to gets changed to the specified value.

box123.bluehost.com is the hostname of the server on which the website is hosted.

So what am I doing wrong? What can I do to get the "From" address the same as the reply-to address?

Is it something I'm doing wrong, or is the web host playing foul?

标签: php email smtp
8条回答
孤独总比滥情好
2楼-- · 2019-01-01 11:54

I had the same Issue, I checked the php.net site. And found the right format.
This is my updated code.

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:  ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" .
            'Reply-To: '.  $fromEmail . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

The \r\n should be in double quotes(") itself, the single quotes(') will not work.

查看更多
低头抚发
3楼-- · 2019-01-01 11:57

I realize this is an old thread, but i had the same problem since i moved to bluehost yesterday. It may not have been the selected answer but i support the bluehost article 206 reply.

I created a valid email in control panel and used it as my From address and it worked.

查看更多
登录 后发表回答