问题描述
我在PHP中遇到了mail()的问题.发送邮件时,它告诉我标题包含格式错误的换行符. 我已经阅读了这个问题,但并不能解决我的问题.我也知道我不能使用\r\r
,\r\0
,\r\n\r\n
,\n\n
或\n\0
,而我没有.但是问题出在哪里呢?我不知道.谢谢您的时间.
I've ran into some problems with mail() in PHP. When sending my mail, it tells me the headers contains malformatted newlines. I've read this question, and it didn't solve my problem. I'm also aware that I can't use \r\r
, \r\0
, \r\n\r\n
, \n\n
, or \n\0
, which I have not. But where's the problem then? I can't figure out. Thanks for your time.
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");
推荐答案
您的问题是,您正在尝试将邮件正文作为标头发送,如对问题的评论中所述.
Your problem is that you're trying to send message body as headers, as mentioned in the comments to your question.
在某些国家/地区尝试通过mail()
发送MIME邮件附件可能被视为酷刑.有很多库可以为您执行此操作,我使用 PEAR Mail_Mime包.
Trying to send MIME mail attachments via mail()
is probably considered torture in some countries. There are plenty of libraries to do this for you, I use the 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()错误:在Additional_header中发现多个或格式错误的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!