简体   繁体   English

Postfix 忽略 PHP mail() 函数

[英]Postfix Ignores PHP mail() function

I'm successfully sending mail using PHP using the mail() function but the sending is showing up as the server address and not what I have configured in the script.我使用 PHP 使用 mail() 函数成功发送邮件,但发送显示为服务器地址,而不是我在脚本中配置的地址。 Postfix is installed on the apache server. Postfix 安装在 apache 服务器上。 On ServerFault answer I read to use the -f and -r flags but that does not work either like this:ServerFault 答案中,我读到使用 -f 和 -r 标志,但这不起作用:

mail($to, $subject, $message, $headers,'From: ' . $fromname . ' <'.$from.'>', "-f $from -r mybounceemail@example.com");

The best I've been able to do is change the Postfix myorigin which is the local machine name hostname to the parent domain of the machine name.我能做的最好的事情是将本地机器名称主机名的 Postfix myorigin 更改为机器名称的父域。

This leads me to believe Postfix is ignoring or stripping the From: element?这让我相信 Postfix 忽略或剥离了 From: 元素?

mail($to, $subject, $message, $headers,'From: ' . $fromname . ' <'.$from.'>', "-f $from -r mybounceemail@example.com");

Cannot work as mail() only accepts 5 parameters: bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )不能工作,因为mail()只接受 5 个参数: bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

For me both对我来说

mail($to, $subject, $message, null, "-r sender@example.com");
mail($to, $subject, $message, 'Name <something@example.com>', "-r sender@example.com");

work (I don't have any additional restrictions in postfix which might rewrite senders).工作(我在 postfix 中没有任何可能重写发件人的额外限制)。 Also make sure that the sender is properly formatted (no spaces or whatsoever).还要确保发件人的格式正确(没有空格或任何其他内容)。

However, I recommend to adjust the sendmail_path setting in your php.ini to include the "-r mybounceemail@example.com" parameter.但是,我建议调整 php.ini 中的sendmail_path设置以包含“-r mybounceemail@example.com”参数。

If it doesn't work for you, you should post the mail headers here and check your log files for warnigns/errors.如果它对您不起作用,您应该在此处发布邮件标题并检查您的日志文件是否有警告/错误。

PS: Please make sure you don't pass user entered data directly to mail() , because if users can insert newlines (such as \\nCC: something@example.com in subject, to mail or from mail) your server might be used for spamming. PS:请确保您不要将用户输入的数据直接传递给mail() ,因为如果用户可以插入换行符(例如\\nCC: something@example.com在主题、邮件或来自邮件中),您的服务器可能会被使用用于垃圾邮件。

You could let the Postfix do its job rewriting headers and adding proper From: addresses.您可以让 Postfix 完成它的工作,重写标头并添加正确的From:地址。 For this, Postfix has sender_canonical_maps .为此,Postfix 有sender_canonical_maps

Let's say your web server (or a process manager) runs under user named username , and you want to have support@example.com in the From: header of your emails instead of unclear username@servername.example.com .假设您的 Web 服务器(或进程管理器)在名为username下运行,并且您希望在电子邮件的From:标题中使用support@example.com而不是不明确的username@servername.example.com Then, canonical map would contain:然后,规范地图将包含:

#/etc/postfix/canonical
username support@example.com

Configuring Postfix to use it can't be easier:配置 Postfix 以使用它再简单不过了:

postmap /etc/postfix/canonical
postconf -e sender_canonical_maps=hash:/etc/postfix/canonical

Other option is to tell postfix to mark all emails send from you server as send from support@example.com by means of luser_relay :其他选项是通过luser_relay告诉 postfix 将从您的服务器发送的所有电子邮件标记为从support@example.com发送:

postconf -ev luser_relay=support@example.com

If you can't do it, quite possible someone already did this for you, but did that wrong.如果你做不到,很可能有人已经为你做了这件事,但做错了。 That could be a reason why you can't change From: address since it is being forcedly overwritten by Postfix.这可能是您无法更改From:地址的原因,因为它被 Postfix 强制覆盖。

You've got the header argument in your mail() call twice and thus, your $additional_parameters argument isn't being used because it's the 6th argument in a function that only accepts 5. You should instead move the header to be included with the rest of your headers:您在mail()调用中获得了 header 参数两次,因此没有使用$additional_parameters参数,因为它是仅接受 5 的函数中的第 6 个参数。您应该改为移动要包含在其余的标题:

//Be mindful to protect from email injection attacks.
$fromname = str_replace("\n", "", $fromname);
$from = str_replace("\n", "", $from);

//This assumes that $headers is already set, containing your other headers
$headers .= 'From: ' . $fromname . ' <'.$from.">\n";

mail($to, $subject, $message, $headers, "-f $from -r mybounceemail@example.com");

Using this, I would expect that your 5th argument is now redundant and could be simplified to:使用这个,我希望你的第 5 个参数现在是多余的,可以简化为:

$fromname = str_replace("\n", "", $fromname);
$from = str_replace("\n", "", $from);

$headers .= 'From: ' . $fromname . ' <'.$from.">\n";

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

In PHP, the syntax for mail function is as follows:在 PHP 中, mail函数的语法如下:

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

So in your case, if you want to specify a different "From" address and a different "Return path" your code should be as follows:因此,在您的情况下,如果您想指定不同的“发件人”地址和不同的“返回路径”,您的代码应如下所示:

$to = "john@example.com";
$subject = "Your reset email";
$message = "<message text / html>";
$from = "support@example.com";
$bounceEmail = "bouncey@example.com";
$headers = array(
    "From: " . $from
);
$headers = implode("\r\n", $headers);
$parameters = "-r" . $bounceEmail;

// Send it!
mail($to, $subject, $message, $headers, $parameters);

Hope this helps!希望这可以帮助!

In my case, the cron was using the wrong instance of php.就我而言,cron 使用了错误的 php 实例。

So you can specify the php instance by doing something like:因此,您可以通过执行以下操作来指定 php 实例:

33 * * * * /usr/bin/php /location/of/file/filename.php >> /var/log/syslog

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

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