带有 www-data 的 PHP 邮件问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5666312/
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 22:08:44  来源:igfitidea点击:

PHP mail issue with www-data

phpsendmail

提问by aherlambang

I am trying to invoke sendmail via PHP's mail function by the following code:

我正在尝试通过以下代码通过 PHP 的邮件功能调用 sendmail:

$to      = '[email protected]';
    $subject = 'test';
    $message = 'test';
    $headers = 'From: [email protected]' . "\r\n" .
               'Reply-To: [email protected]' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);

However in my mail.log I am getting a message that the from is not the address I specified in the header:

但是,在我的 mail.log 中,我收到一条消息,指出 from 不是我在标题中指定的地址:

<www-data@Name>: Sender address rejected: Domain not found

Why is this?? I am running PHP's fast-cgi on ubuntu Why doesn't sendmail use the header that I have specified via the PHP code?

为什么是这样??我在 ubuntu 上运行 PHP 的 fast-cgi 为什么 sendmail 不使用我通过 PHP 代码指定的标头?

回答by anubhava

It looks like www-data@Nameis your envelope "from" address. The envelope "from" address is differentfrom the address that appears in your "From:" header of the email. It is what sendmail uses in its "MAIL FROM/RCPT TO" exchange with the receiving mail server.The main reason it is called an "envelope" address is that appears outsideof the message header and body, in the raw SMTP exchange between mail servers.

看起来www-data@Name是您的信封“发件人”地址。信封“发件人”地址与电子邮件“发件人:”标题中显示的地址不同。这就是发生在其“MAIL FROM / RCPT TO”与接收邮件server.The主要原因,它被称为“信封”地址交换Sendmail使用是出现外界消息标题和正文,邮件的原始SMTP交换服务器。

The default envelope "from" address on unix depends on what sendmail implementation you are using. But typically it will be set to the username of the running process followed by "@" and the hostname of the machine. In a typical configuration this will look something like [email protected].

unix 上的默认信封“发件人”地址取决于您使用的 sendmail 实现。但通常它会被设置为正在运行的进程的用户名,后跟“@”和机器的主机名。在典型配置中,这将类似于[email protected].

If your emails are being rejected by receiving mail servers, or if you need to change what address bounce emails are sent to, you can change the envelope "from" address to solve your problems.

如果您的邮件被接收邮件服务器拒绝,或者您需要更改退回邮件的发送地址,您可以更改信封“发件人”地址来解决您的问题。

To change the envelope "from" address on unix, you specify an "-r" option to your sendmail binary. You can do this globally in php.ini by adding the "-r" option to the "sendmail_path" command line. You can also do it programmatically from within PHP by passing -r [email protected]as the additional parameterargument to the mail() function (the 5th argument). If you specify an address in both places, the sendmail binary will be called with two "-r" options, which may have undefined behavior depending on your sendmail implementation. With the Postfix MTA, later "-r" options silently override earlier options, making it possible to set a global default and still get sensible behavior when you try to override it locally.

要在 unix 上更改信封“发件人”地址,请为 sendmail 二进制文件指定“-r”选项。您可以在 php.ini 中通过将“-r”选项添加到“sendmail_path”命令行来全局执行此操作。您还可以通过将附加参数-r [email protected]作为附加参数传递给 mail() 函数(第 5 个参数),从 PHP 内部以编程方式执行此操作。如果您在两个地方都指定了地址,则将使用两个“-r”选项调用 sendmail 二进制文件,这可能具有未定义的行为,具体取决于您的 sendmail 实现。使用 Postfix MTA,以后的“-r”选项会默默地覆盖以前的选项,从而可以设置全局默认值,并且当您尝试在本地覆盖它时仍然可以获得合理的行为。

EDIT

编辑

About optional flags that can be passed to sendmail:-fwill set the From address, -rwill override the default Return-path that sendmail generates (typically the From address gets used). If you want your bounce-backs to go to a different address than the from address, try using both flags at once: -f [email protected] -r [email protected]

关于可以传递给 sendmail 的可选标志:-f将设置发件人地址,-r将覆盖 sendmail 生成的默认返回路径(通常使用发件人地址)。如果您希望您的退回邮件转到与发件人地址不同的地址,请尝试同时使用这两个标志:-f [email protected] -r [email protected]

my php.ini

我的 php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log =

回答by David G

Although this is an old question, I'm adding this answer in case it is of help to someone:

虽然这是一个老问题,但我添加了这个答案,以防它对某人有帮助:

I had the same problem with the From:header being re-written to www-data@host...I eventually tracked it down to the ssmtp bridge service that was piping mail from our web server into our mailserver. I added the line FromLineOverride=YESin the file /etc/ssmtp/ssmtp.confand the problem disappeared.

我在From:重新写入标头时遇到了同样的问题,www-data@host...我最终将其追踪到了 ssmtp 桥接服务,该服务将邮件从我们的 Web 服务器传送到我们的邮件服务器。我FromLineOverride=YES在文件中添加了这一行,/etc/ssmtp/ssmtp.conf问题就消失了。

回答by DependencyHell

In my case, I've got a hosted server so I needed to edit this file :

就我而言,我有一个托管服务器,所以我需要编辑这个文件:

/etc/ssmtp/ssmtp.conf

/etc/ssmtp/ssmtp.conf

Then uncomment this line :

然后取消注释这一行:

FromLineOverride=YES

FromLineOverride=YES

Once done, personals headers are working.

完成后,个人标题正在工作。

回答by Mirous

I was having similar problem with www-data when all my mails were sent and received with this header:

当我使用此标题发送和接收所有邮件时,我遇到了与 www-data 类似的问题:

From: www-data <[email protected]>

I used the -f [email protected]flag as 5th argument with the PHP email() function (as mentioned in accepted answer), but i was still receiving my emails as:

我使用该-f [email protected]标志作为 PHP email() 函数的第 5 个参数(如已接受的答案中所述),但我仍然收到我的电子邮件:

From: www-data <[email protected]>

So i added one more flag -f [email protected] -F infoto set the full name of the email and finally i was getting emails as i wanted:

所以我又添加了一个标志-f [email protected] -F info来设置电子邮件的全名,最后我收到了我想要的电子邮件:

From: info <[email protected]>

I'm posting this answer because nobody mentions it here and i got a little stuck on it.

我发布这个答案是因为没有人在这里提到它,而且我对此有点卡住了。

回答by Paul Spiegel

I had this issue using exim4with smarthost. The mails were sent with

我在使用exim4smarthost 时遇到了这个问题。这些邮件是用

Return-path: <www-data@servername>

which was rejected by the ISP. I needed to change it to at least [email protected](assuming 'example.com' is the servers public domain name). I could achieve that by changing /etc/mailnamefrom

被ISP拒绝了。我需要将其更改为至少[email protected](假设“example.com”是服务器公共域名)。我可以通过改变/etc/mailname来实现这一点

servername

to

example.com

This has already worked for me, having [email protected]as Return-path.

这已经对我有用,[email protected]因为Return-path.

However - If you want to completely change the email address, you can configure it in /etc/email-addressesas

但是 - 如果您想完全更改电子邮件地址,您可以将其配置/etc/email-addresses

www-data: [email protected]

After that the emails have been sent by default with

之后,默认情况下电子邮件已发送

Return-path: <[email protected]>

回答by Martin

This worked for me:

这对我有用:

$mail->Sendmail = $mail->Sendmail.' -f '.$mail_errorsto;