php php邮件“发件人”标题的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2014081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:41:32  来源:igfitidea点击:

problem with php mail 'From' header

phpemailsmtp

提问by jrharshath

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

我正在建立一个网站,当他注册时向用户发送和发送电子邮件。

My code (the gist of it):

我的代码(它的要点):

<?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.";
?> 

the problem is that when the mail is delivered, the from header remains [email protected], while reply-to gets changed to the specified value.

问题是当邮件被传递时, from 标头仍然存在[email protected],而 reply-to 被更改为指定的值。

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

box123.bluehost.com是托管网站的服务器的主机名。

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?

是我做错了什么,还是网络主机犯规了?

回答by Pekka

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.

编辑:我刚刚注意到您正在尝试使用 gmail 地址作为 from 值。这是行不通的,ISP 覆盖它是正确的。如果要将回复重定向到外发邮件,请使用reply-to.

A workaround for valid addresses that works with many ISPs:

适用于许多 ISP 的有效地址的解决方法:

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

尝试向您的mail()命令添加第五个参数:

mail($to,$subject,$message,$headers,"-f [email protected]");

回答by John Connolly

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

原来发帖人的服务器(blueHost)有一个关于这个问题的常见问题解答

Article 206.

第 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.

这是因为我们的服务器要求您(或您的脚本)在电子邮件标题中使用格式正确、有效的 From: 字段。如果发件人:字段格式不正确、为空或 cPanel 中不存在电子邮件地址,则发件人:地址将更改为username@box###.bluehost.com

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

您必须更改正在使用的脚本才能正确使用有效的 From: header.

Examples of headers that should work would be:

应该工作的标题示例是:

From: [email protected]
From: "user" <[email protected]>

发件人:[email protected]
发件人:“用户”<[email protected]>

Examples of headers that will NOT work:

不起作用的标题示例:

From: "[email protected]"
From: user @ domain.com
From: [email protected] <[email protected]>

自: “[email protected]
从:用户@ domain.com
从:[email protected] <[email protected]>

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.

对于 Joomla 和 Wordpress 等脚本,您需要按照他们的文档正确格式化 from 字段。Wordpress 将需要 Mail From 插件。

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

注意:您使用的电子邮件地址必须是在 cPanel 中创建的有效帐户。

回答by Eugine Joseph

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

我有同样的问题,我检查了 php.net 站点。并找到了正确的格式。
这是我更新的代码。

$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\nshould be in double quotes(")itself, the single quotes(')will not work.

\ r \ n应在双引号(“)本身,单引号(')将无法正常工作。

回答by Luís Guilherme

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

为了防止网络钓鱼,一些邮件服务器会阻止 From 被重写。

回答by Rvenca

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.

我意识到这是一个旧线程,但是自从我昨天搬到 bluehost 以来,我遇到了同样的问题。它可能不是选定的答案,但我支持 bluehost 文章 206 的回复。

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

我在控制面板中创建了一个有效的电子邮件并将其用作我的发件人地址并且它起作用了。

回答by Anahit DEV

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

我通过在 Cpanel 中添加电子邮件帐户并将相同的电子邮件添加到来自字段的标题中解决了这个问题

$header = 'From: XXXXXXXX <[email protected]>' . "\r\n";

回答by zeeshan

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!

标题在我的共享主机上对我不起作用,原因是我在标题中使用了我的 hotmail 电子邮件地址。我在我的 cpanel 上创建了一封电子邮件,并在标题中设置了相同的电子邮件,是的,它就像一个魅力!

 $header = 'From: ShopFive <[email protected]>' . "\r\n";

回答by symcbean

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.

如果您真的想从“@gmail.com”发送邮件,为什么不直接使用 gmail SMTP 服务?如果您无法重新配置运行 PHP 的服务器,那么有很多电子邮件包装工具可以让您指定自定义 SMTP 中继 phpmailer。

C.

C。