在 PHP 上使用 mail() 发送电邮,如果没有正确设定 Return-Path, 发出的邮件会使用系统的主机名称,例如 apache@hostname.server, 以下是程式码:
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $from = "from@example.com"; $to = "to@example.com"; $header = "FROM: " . $from . "\r\n". "Reply-To: " . $from . "\r\n". "Return-Path: " . $from . "\r\n". "Message-ID: <" . time() . "." . $from . ">". mail($to, "email subject", "The email body", $header); ?> |
在收到的邮件会看到寄件者是 from@example.com, 但当按下回复时,会使用系统的主机名称,除了回复地址名,也会有部邮 email server 拒收,会在 email server 的纪录档出现 “invalid sender MX”。
原因是邮件的 Return-Path 并没有使用 PHP 的 Return-Path 设定,邮件的 source 可以看到:
Return-Path:
Delivered-To:
要解决只要在 mail() 函式加入多一个参数,用 sendmail 的 -f 参数设定 Return Path, 具体写法如下:
|
1 2 3 4 5 6 7 8 9 10 |
<?php $from = "from@example.com"; $to = "to@example.com"; $header = "FROM: " . $from . "\r\n". "Reply-To: " . $from . "\r\n". "Message-ID: <" . time() . "." . $from . ">". mail($to, "email subject", "The email body", $header, "-f".$from); ?> |