简体   繁体   English

php的mail()覆盖了“发件人”电子邮件地址

[英]php's mail() is overriding the 'from' email address

I'm using PHP's mail() -- my emails from our site get sent and received successfully. 我正在使用PHP的mail()-来自我们网站的电子邮件已成功发送和接收。

User "theArtist" puts a painting up for sale on our site. 用户“ theArtist”将一幅画作出售给我们。 User "RichGuy" sees it, and our site has a form where RichGuy types his email address and a message to "theArtist." 用户“ RichGuy”看到了,我们的网站具有一个表单,其中RichGuy键入他的电子邮件地址和给“ theArtist”的消息。

When "RichGuy" fills out this form and clicks 'send', our PHP code retrieves the artist's email address from the database and makes that the "To" email address used in the call to mail(). 当“ RichGuy”填写此表单并单击“发送”时,我们的PHP代码将从数据库中检索艺术家的电子邮件地址,并使该“收件人”电子邮件地址用于对mail()的调用。 And since RichGuy just entered his reply email on that form, "RichGuy@gmail.com" is used for the "From: " header in the call to mail(). 由于RichGuy刚在该表格上输入了他的回复电子邮件,因此mail()调用中的“ From:”标头使用“ RichGuy@gmail.com”。

I set up two dummy email accounts to test our mail() code, one to send mail (RichGuy@gmail.com and one to receive mail (theArtist@hotmail.com). 我设置了两个虚拟电子邮件帐户来测试我们的mail()代码,一个用于发送邮件(RichGuy@gmail.com),另一个用于接收邮件(theArtist@hotmail.com)。

My real name is the problem, because it shows up in the 'From' part in the recipient's received message. 我的真实姓名是问题所在,因为它显示在收件人的收到邮件的“发件人”部分中。 My real name and my hotmail email are FredSmith@hotmail.com . 我的真实姓名和hotmail电子邮件为FredSmith@hotmail.com NOTE THAT "theArtist" also is a hotmail.com account, not sure if that has a bearing here. 注意“ theArtist”也是hotmail.com帐户,不确定此处是否有影响。 I've had my FredSmith@hotmail.com account for many years now. 我已经有我的FredSmith@hotmail.com帐户很多年了。 The other two 'dummy' accounts are new, just for testing this mail() code. 其他两个“虚拟”帐户是新帐户,仅用于测试此mail()代码。

Here's the PHP code: 这是PHP代码:

 $sendTo = "theArtist@hotmail.com";
 $subject = "I want that painting now!";
 $theMsg =  "That's a fine abstract piece, I'll buy it.";
 $headers = "From: RichGuy@gmail.com";

 if(mail($sendTo, $subject, $theMsg, $headers))
 {
       // success
 }

The above email is successfully being received by theArtist@hotmail.com -- here's what "theArtist" receives: 上述电子邮件已成功通过theArtist@hotmail.com接收-这是“ theArtist”收到的:


I want that painting now! 我现在要那幅画!

Fred Smith (RichGuy@gmail.com) 弗雷德·史密斯 (RichGuy@gmail.com)

To: theArtist@hotmail.com 至:theArtist@hotmail.com

From: RichGuy@gmail.com 来自:RichGuy@gmail.com

Sent: Tue 9/24/13 8:20 PM 发送:13年9月24日星期二晚上8:20

To: theArtist@hotmail.com 至:theArtist@hotmail.com

That's a fine abstract piece, I'll buy it. 那是一个很好的抽象作品,我会购买。


Why in the heck does my real name, Fred Smith, appear in theArtist@hotmail.com's email like this? 为什么我的真名Fred Fred这样出现在theArtist@hotmail.com的电子邮件中? Is mail() behind the scenes, despite my using the 2 dummy email accounts, authenticating using my real name? 尽管我使用了2个虚拟电子邮件帐户,但mail()是否在后台使用我的真实姓名进行身份验证? I see the same behavior on both my localhost web server and our staging server running live on the web. 我在本地主机Web服务器和在网络上实时运行的登台服务器上都看到了相同的行为。

So I tried to force the issue as follows: 因此,我尝试将问题强制如下:

 $sendTo = "theArtist@hotmail.com";
 $subject = "I want a deal on that painting";
 $theMsg =  "How about a discount on your painting?";
 $headers = "From: RichGuy@gmail.com";
 if(mail($sendTo, $subject, $theMsg, $headers, '-fRichGuy@gmail.com'))

NO CHANGE. 没变。 My real name , Fred Smith, is somehow tacked on right before the sender's email address, RichGuy@gmail.com -- 我的真名 Fred Fred在发送者的电子邮件地址RichGuy@gmail.com之前加上了某种方式-


I want a deal on that painting 我想买那幅画

Fred Smith (RichGuy@gmail.com) 弗雷德·史密斯 (RichGuy@gmail.com)

To: theArtist@hotmail.com 至:theArtist@hotmail.com

From: RichGuy@gmail.com 来自:RichGuy@gmail.com

Sent: Tue 9/24/13 8:20 PM 发送:13年9月24日星期二晚上8:20

To: theArtist@hotmail.com 至:theArtist@hotmail.com

How about a discount on your painting? 您的画有折扣吗?


Could this be an authentication issue somehow? 这可能是身份验证问题吗?
Is it possible that mail() recognizes "ah-HAH, the real 'owner' of the site is Fred Smith, so we cannot authenticate on RichGuy@gmail.com nor on theArtist@hotmail.com, we have to authenticate prior to emailing on FredSmith." mail()是否可能识别“ ah-HAH,网站的真正'所有者'是Fred Smith,因此我们无法通过RichGuy@gmail.com或theArtist@hotmail.com进行身份验证,我们必须在通过电子邮件进行身份验证之前在弗雷德·史密斯上。”

By the way, I checked the sendmail debug.log on my localhost web server and there is NO "Fred Smith" logged there with the email messages. 顺便说一句,我在本地主机Web服务器上检查了sendmail debug.log,并且没有“ Fred Smith”和电子邮件一起登录。

Check the PHP.ini [mail function] configuration section, my guess is that it appears as per below: 检查PHP.ini [mail function]配置部分,我猜它如下所示:

[mail function]
SMTP = smtp.live.com
smtp_port = 25
auth_username = FredSmith@hotmail.com
auth_password = fredspassword

The outgoing mail is most likely authenticating with Hotmail using your normal account, and then adding a Sender header. 外发邮件很可能使用您的普通帐户通过Hotmail进行身份验证,然后添加发件人标头。 Many ISPs now block SMTP so this is often the only way to SMTP from a local dev environment. 现在,许多ISP都阻止SMTP,因此,这通常是从本地开发环境访问SMTP的唯一方法。

Here's how I solved this: 这是我解决此问题的方法:

$sendername = strtok($senderEmail, "@");
$headers = "From: " . $sendername . " <" . $senderEmail . ">\r\n";

I simply took the sender's email 'handle' up to the '@' sign and formatted the mail 'From' header using the format "theSendersName " and it worked. 我只是简单地将发件人的电子邮件“句柄”移至“ @”符号,并使用“ theSendersName”格式对邮件“发件人”标头进行了格式化,并且可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM