PHP 中的 mail() 错误:在 additional_header 中发现多个换行符或格式错误的换行符
mail() error in PHP: Multiple or malformed newlines found in additional_header
我 运行 在 PHP 中遇到了 mail() 的一些问题。发送邮件时,它告诉我邮件头包含格式错误的换行符。 ,并没有解决我的问题。我也知道我不能使用 \r\r
、\r[=14=]
、\r\n\r\n
、\n\n
或 \n[=17=]
,我没有使用过。但是问题出在哪里呢?我想不通。感谢您的宝贵时间。
function mail_attachment($filename, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file_size = filesize($filename);
$handle = fopen($filename, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n";
$header .= $content."\r\n";
$header .= "--".$uid."--";
mail($mailto, $subject, $message, $header)
}
mail_attachment("invoice/0.pdf", "customer@customer.com", "noreply@mattronic.dk", "Mattronic", "reply@mattronic.dk", "Invoice", "Describing text");
您的问题是您正尝试将消息 body 作为 headers 发送,如您问题的评论中所述。
尝试通过 mail()
发送 MIME 邮件附件在某些国家/地区可能被视为酷刑。有很多库可以为你做这件事,我使用 PEAR Mail_Mime package.
function mail_attachment($filename, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
include("Mail.php");
include("Mail/mime.php");
$headers = [
"To"=>$mailto,
"From"=>"$from_name <$from_mail>",
"Reply-To"=>$replyto
"Subject"=>$subject,
"Date"=>date(DATE_RFC822),
];
$msg = new Mail_mime();
$mail =& Mail::factory("smtp");
$msg->setTXTBody($message);
$msg->addAttachment(file_get_contents($filename), "application/pdf", basename($filename), false);
$body = $msg->get();
$headers = $msg->headers($headers);
$mail->send($email_address, $headers, $body);
}
我 运行 在 PHP 中遇到了 mail() 的一些问题。发送邮件时,它告诉我邮件头包含格式错误的换行符。 \r\r
、\r[=14=]
、\r\n\r\n
、\n\n
或 \n[=17=]
,我没有使用过。但是问题出在哪里呢?我想不通。感谢您的宝贵时间。
function mail_attachment($filename, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file_size = filesize($filename);
$handle = fopen($filename, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n";
$header .= $content."\r\n";
$header .= "--".$uid."--";
mail($mailto, $subject, $message, $header)
}
mail_attachment("invoice/0.pdf", "customer@customer.com", "noreply@mattronic.dk", "Mattronic", "reply@mattronic.dk", "Invoice", "Describing text");
您的问题是您正尝试将消息 body 作为 headers 发送,如您问题的评论中所述。
尝试通过 mail()
发送 MIME 邮件附件在某些国家/地区可能被视为酷刑。有很多库可以为你做这件事,我使用 PEAR Mail_Mime package.
function mail_attachment($filename, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
include("Mail.php");
include("Mail/mime.php");
$headers = [
"To"=>$mailto,
"From"=>"$from_name <$from_mail>",
"Reply-To"=>$replyto
"Subject"=>$subject,
"Date"=>date(DATE_RFC822),
];
$msg = new Mail_mime();
$mail =& Mail::factory("smtp");
$msg->setTXTBody($message);
$msg->addAttachment(file_get_contents($filename), "application/pdf", basename($filename), false);
$body = $msg->get();
$headers = $msg->headers($headers);
$mail->send($email_address, $headers, $body);
}