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:37

It turns out the original poster's server (blueHost) has a FAQ concerning this very question.

Article 206.


This is because our servers require you (or your script) to use a properly formatted, valid From: field in the email's header. If the From: field is not formatted correctly, empty or the email address does not exist in the cPanel, the From: address will be changed to username@box###.bluehost.com.

You must change the script you are using to correctly use a valid From: header.

Examples of headers that should work would be:

From: user@domain.com
From: "user" <user@domain.com>

Examples of headers that will NOT work:

From: "user@domain.com"
From: user @ domain.com
From: user@domain.com <user@domain.com>

Our servers will not accept the name for the email address and the email address to be the same. It will not accept a double declaration of the email address.

For scripts such as Joomla and Wordpress, you will need to follow their documentation for formatting the from fields properly. Wordpress will require the Mail From plugin.

Note: The email address you use must be a valid created account in the cPanel.

查看更多
春风洒进眼中
3楼-- · 2019-01-01 11:38

headers were not working for me on my shared hosting, reason was i was using my hotmail email address in header. i created a email on my cpanel and i set that same email in the header yeah it worked like a charm!

 $header = 'From: ShopFive <site@mysite.org>' . "\r\n";
查看更多
初与友歌
4楼-- · 2019-01-01 11:46

In order to prevent phishing, some mail servers prevent the From from being rewritten.

查看更多
时光乱了年华
5楼-- · 2019-01-01 11:49

I solved this by adding email accounts in Cpanel and also adding that same email to the header from field like this

$header = 'From: XXXXXXXX <test@test.org>' . "\r\n";
查看更多
高级女魔头
6楼-- · 2019-01-01 11:51

The web host is not really playing foul. It's not strictly according to the rules - but compared with some some of the amazing inventions intended to prevent spam, its not a particularly bad one.

If you really do want to send mail from '@gmail.com' why not just use the gmail SMTP service? If you can't reconfigure the server where PHP is running, then there are lots of email wrapper tools out there which allow you to specify a custom SMTP relay phpmailer springs to mind.

C.

查看更多
其实,你不懂
7楼-- · 2019-01-01 11:52

Edit: I just noted that you are trying to use a gmail address as the from value. This is not going to work, and the ISP is right in overwriting it. If you want to redirect the replies to your outgoing messages, use reply-to.

A workaround for valid addresses that works with many ISPs:

try adding a fifth parameter to your mail() command:

mail($to,$subject,$message,$headers,"-f your@email.here");
查看更多
登录 后发表回答