PHP 邮件 - 找到多个或格式错误的换行符


PHP Mail - Multiple or malformed newlines found

我们升级了我们的PHP版本,现在收到错误" 警告:mail():在additional_header中找到多个或格式错误的换行符"。

我已经创建了不同事物的实验,但没有任何东西起作用。我很抱歉,因为我不太熟悉所有这些是如何工作的,所以请耐心等待。

目标(在早期版本中有效)是发送基于 HTML 的消息(具有、
标签等),其中包含我们服务器上存在的 PDF 附件。

如果您能给我具体的调整,我将不胜感激!

$sFrom = "[Company Name] <[Our Email]>";
$sReplyTo = "[Our Email]";
$sParams = "-f [Our Email]";
$attachment = chunk_split(base64_encode(file_get_contents($sPath)));
$uid = md5(uniqid(time()));
$sHeaders = "From: ".$sFrom."'n".
            "Reply-To: ".$sReplyTo."'n".
            "MIME-Version: 1.0'n".
            "Content-Type: multipart/mixed; boundary='"".$uid."'"'n'n".
            "This is a multi-part message in MIME format.'n".
            "--".$uid."'n".
            "Content-Type: text/html; charset='iso-8859-1''n".
            "Content-Transfer-Encoding: 7bit'n'n".
            $sMessage."'n'n".
            "--".$uid."'n".
            "Content-Type: application/pdf; name='"".$sFileName."'"'n".
            "Content-Transfer-Encoding: base64'n".
            "Content-Disposition: attachment; filename='"".$sFileName."'"'n'n".
            $attachment."'n'n".
            "--".$uid."--";    
if (!mail($sTo, $sSubject, "", $sHeaders, $sParams)) {
    $bError = true;
}

尝试使用 '''n 而不是 'n

因为 https://bugs.php.net/bug.php?id=68776 不再允许使用多个换行符(或目前?尝试切换到PEAR Mailer,PHPMailer或其他东西。

需要多个换行符"'n'n"才能发送带有带有 mail() 附件的 php 邮件...

1) 尝试附加到未初始化的变量$sMessage时,您将收到错误。还要检查其他变量是否具有预期的内容($sTo、$sSubject)。

2)您将邮件内容从标题移动到邮件中,但忘记将其添加到邮件功能if (!mail($sTo, $sSubject, ", $sHeaders, $sParams))将成为if (!mail($sTo, $sSubject, $sMessage, $sHeaders, $sParams))

3)在"多部分/混合"之后您仍然有多个换行符

4)删除"这是MIME格式的多部分邮件"行在这里,我更新了您的代码:

$sFrom = "me@aju.ro";
$sReplyTo = "me@aju.ro";
$sParams = "-f me@aju.ro";
$attachment = chunk_split(base64_encode(file_get_contents($sPath)));
$uid = md5(uniqid(time()));
$sHeaders = "From: ".$sFrom."'n".
            "Reply-To: ".$sReplyTo."'n".
            "MIME-Version: 1.0'n".
            "Content-Type: multipart/mixed; boundary='"".$uid."'"'n".
            "--".$uid."'n".
            "Content-Type: text/html; charset='iso-8859-1''n".
            "Content-Transfer-Encoding: 7bit'n'n".
$sMessage="'n'n".
            "--".$uid."'n".
            "Content-Type: application/pdf; name='"".$sFileName."'"'n".
            "Content-Transfer-Encoding: base64'n".
            "Content-Disposition: attachment; filename='"".$sFileName."'"'n'n".
            $attachment."'n'n".
            "--".$uid."--";    
if (!mail($sReplyTo, $sSubject, $sMessage, $sHeaders, $sParams)) {
    $bError = true;
}

这是 php 向上分级的变化。PHP mail() 函数中的安全风险已得到修复,不再允许使用额外的换行符。

请在additional_headers参数中删除多个换行符。这些计数为"多个或格式错误的换行符":

''r''r、''r''0、''r'''r'、''、'''0。

在 SMTP 标准 https://www.rfc-editor.org/rfc/rfc2045#section-2.1 中,换行符定义为'r'n