繁体   English   中英

Postfix 忽略 PHP mail() 函数

[英]Postfix Ignores PHP mail() function

我使用 PHP 使用 mail() 函数成功发送邮件,但发送显示为服务器地址,而不是我在脚本中配置的地址。 Postfix 安装在 apache 服务器上。 ServerFault 答案中,我读到使用 -f 和 -r 标志,但这不起作用:

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

我能做的最好的事情是将本地机器名称主机名的 Postfix myorigin 更改为机器名称的父域。

这让我相信 Postfix 忽略或剥离了 From: 元素?

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

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

对我来说

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

工作(我在 postfix 中没有任何可能重写发件人的额外限制)。 还要确保发件人的格式正确(没有空格或任何其他内容)。

但是,我建议调整 php.ini 中的sendmail_path设置以包含“-r mybounceemail@example.com”参数。

如果它对您不起作用,您应该在此处发布邮件标题并检查您的日志文件是否有警告/错误。

PS:请确保您不要将用户输入的数据直接传递给mail() ,因为如果用户可以插入换行符(例如\\nCC: something@example.com在主题、邮件或来自邮件中),您的服务器可能会被使用用于垃圾邮件。

您可以让 Postfix 完成它的工作,重写标头并添加正确的From:地址。 为此,Postfix 有sender_canonical_maps

假设您的 Web 服务器(或进程管理器)在名为username下运行,并且您希望在电子邮件的From:标题中使用support@example.com而不是不明确的username@servername.example.com 然后,规范地图将包含:

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

配置 Postfix 以使用它再简单不过了:

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

其他选项是通过luser_relay告诉 postfix 将从您的服务器发送的所有电子邮件标记为从support@example.com发送:

postconf -ev luser_relay=support@example.com

如果你做不到,很可能有人已经为你做了这件事,但做错了。 这可能是您无法更改From:地址的原因,因为它被 Postfix 强制覆盖。

您在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");

使用这个,我希望你的第 5 个参数现在是多余的,可以简化为:

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

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

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

在 PHP 中, mail函数的语法如下:

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

因此,在您的情况下,如果您想指定不同的“发件人”地址和不同的“返回路径”,您的代码应如下所示:

$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);

希望这可以帮助!

就我而言,cron 使用了错误的 php 实例。

因此,您可以通过执行以下操作来指定 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