
本文详解如何在 Laravel + Postfix 邮件转发场景中,不改动原始邮件任何字节(包括换行、边界、空行、MIME 结构),仅修改 SMTP envelope 的 return-path(用于 SRS),从而确保 DKIM body hash 验证通过、DMARC 策略不失效。
本文详解如何在 laravel + postfix 邮件转发场景中,**不改动原始邮件任何字节(包括换行、边界、空行、mime 结构)**,仅修改 smtp envelope 的 return-path(用于 srs),从而确保 dkim body hash 验证通过、dmarc 策略不失效。
在构建符合 DMARC 严格策略(如 p=quarantine 或 p=reject)的邮件转发服务时,一个核心挑战是:任何对原始邮件正文的微小改动(哪怕一个额外换行符、空格或边界前后空白)都会导致 DKIM 签名失效。这是因为 DKIM 的 bh=(body hash)字段基于 RFC 5322 规范下的 canonicalized 原始消息体计算——而该规范化过程对 CRLF、空行、MIME boundary 格式极为敏感。
你当前使用的 PhpMimeMailParser + Swift_Mailer/Mailable 流程本质上是「解析→重构→重序列化」,这必然引入不可控的格式偏差(如自动补全缺失的 Content-Transfer-Encoding、标准化 boundary 分隔符、HTML 正文后插入空行等)。即使手动还原 header 和 boundary,也无法 100% 复现原始字节流 —— 这正是 DKIM 验证失败的根本原因。
✅ 正确解法不是“重建邮件”,而是 “透传原始字节 + 替换 envelope”。即:
- 保留从 Postfix pipe 接收到的完整原始 .eml 文件(含所有 CRLF、空行、二进制附件、嵌套 multipart 结构);
- 仅在 SMTP 协议层修改 MAIL FROM:(即 envelope sender / return-path),注入 SRS 编码后的地址;
- 其余所有内容(包括 To/From/Subject header、全部 MIME body、附件二进制数据)一字不改地原样传输。
✅ 推荐方案:使用 PHPMailer 的 mail() 或 sendmail 模式 + 自定义 envelope
虽然 PHPMailer 默认会解析并重写邮件,但它支持 raw message passthrough 模式:
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
$mail->isSendmail(); // 使用系统 sendmail(实际走 Postfix)
$mail->setFrom('original@example.com', '', false); // 第三个参数 false:不验证/不重写 From header
$mail->addAddress('recipient@forwarded.com');
// ⚠️ 关键:直接设置原始原始邮件体(必须是完整 RFC 5322 格式字符串)
$rawEmail = file_get_contents('/path/to/raw.eml'); // 从 pipe 接收的原始字节流
$mail->msgHTML($rawEmail, '', false); // 第三个参数 false:跳过 HTML 解析,直接作为 raw body
// 强制覆盖 envelope sender(SRS 地址)
$mail->Sender = 'SRS0=XXXX=YY=sender.org=original@example.com';
// 发送(Postfix 将使用 $mail->Sender 作为 MAIL FROM)
$mail->send();? 注意:msgHTML($raw, '', false) 在此上下文中并非渲染 HTML,而是将 $raw 作为原始消息体注入,并禁用所有内容重写逻辑。务必确保 $rawEmail 是 未经任何 trim()、str_replace()、fopen("r") 二进制读取错误处理 的原始字节流。
⚠️ 更稳健方案:绕过 PHPMailer,直接调用 sendmail -f(推荐生产环境)
Postfix 的 sendmail 命令行接口天然支持 envelope 修改,且完全不触碰邮件体:
# 示例 shell 命令(PHP 中用 proc_open 安全执行) /usr/sbin/sendmail -f 'SRS0=XXXX=YY=sender.org=original@example.com' -- recipient@forwarded.com
对应 PHP 实现:
function forwardRawEmail(string $rawEmail, string $srsSender, array $recipients): bool {
$descriptorspec = [
['pipe', 'r'], // stdin
['pipe', 'w'], // stdout
['pipe', 'w'], // stderr
];
$cmd = '/usr/sbin/sendmail -f ' . escapeshellarg($srsSender) . ' -- '
. implode(' ', array_map('escapeshellarg', $recipients));
$process = proc_open($cmd, $descriptorspec, $pipes);
if (!is_resource($process)) {
throw new RuntimeException('Failed to spawn sendmail');
}
fwrite($pipes[0], $rawEmail); // ⚡ 原样写入原始字节流
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$returnCode = proc_close($process);
if ($returnCode !== 0) {
error_log("Sendmail failed (code {$returnCode}): {$stderr}");
return false;
}
return true;
}
// 调用示例(Laravel Mailable 中可封装为 job)
$raw = file_get_contents('php://stdin'); // Postfix pipe 输入
forwardRawEmail($raw, 'SRS0=...@yourdomain.com', ['user@external.com']);❌ 不推荐的方案及原因
- fsockopen + 手动 SMTP 对话:虽可行,但需自行处理 TLS、认证、重试、超时、队列持久化,运维成本高,易引入连接泄漏和状态不一致问题;
- mail() 函数:无法指定 envelope sender(-f 参数在多数托管环境被禁用),且 header 注入不可靠;
- DKIM 重新签名:需你控制源域私钥,违背“转发方不应代表原始发件人”的安全原则,且破坏发件人原有域名信誉链。
✅ 最佳实践总结
| 项目 | 推荐做法 |
|---|---|
| 输入来源 | 从 Postfix pipe 以 binary safe 方式读取(file_get_contents('php://stdin')) |
| Envelope 修改 | 使用 sendmail -f 或 PHPMailer->Sender,绝不修改 headers 中的 From: / Return-Path: |
| Body 处理 | 零解析、零转码、零 trim、零换行标准化 —— fwrite($pipes[0], $raw) 是唯一安全操作 |
| SRS 集成 | 在 envelope sender 中注入 SRS 编码值(如 SRS0=HASH=DOMAIN=original@sender.com),header 保持原始不变 |
| 日志与监控 | 记录原始邮件 SHA256 hash 与转发后接收端验证结果,用于快速定位 DKIM 失败根源 |
遵循以上方案,你将实现真正意义上的「字节级透传转发」,既满足 SRS 的 bounce 回传需求,又完全兼容 DKIM/DMARC 的严格验证要求 —— 因为邮件体从未离开原始服务器的字节上下文。










