在意识到更好的解决方案的同时,我正在使用手动PHP脚本将文件附加到要通过电子邮件发送的消息上。该文件已正确附加,我什至可以看到该消息,但是由于某种原因原始消息($msg
)折叠了!所有这些\n\r
我都消失了。有任何想法吗??
这是我的代码:
// construct message
$msg = "Beginning of message \n -------------------- \n\n";
$msg .= "some logic is added to the message using a loop with \r\n in between each item on the list.\r\n";
$msg .= "--\n The end of the message.";
// handle file upload
$attachment = $_FILES['uploaded']['tmp_name'];
$att_type = $_FILES['uploaded']['type'];
$att_name = $_FILES['uploaded']['name'];
if (is_uploaded_file($attachment)) {
// read the file to be attached ('rb' = read binary)
$file = fopen($attachment, 'rb');
$data = fread($file, filesize($attachment));
fclose($file);
// generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x[$semi_rand]x";
// add the headers for a file attachment
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\"\r\n\r\n";
// MESSAGE GETS WRITTEN HERE
// add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\r\n" .
"--{$mime_boundary}\r\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\r\n\r\n";
$message .= $msg . "\r\n\r\n";
// add file attachment to the message
$message .= "\r\n--{$mime_boundary}\r\n" .
"Content-Type: {$att_type};\r\n" .
" name=\"{$att_name}\"\n" .
"Content-Disposition: attachment;" .
" filename=\"{$att_name}\"\r\n\r\n" .
$data . "\r\n" .
"--{$mime_boundary}--\n";
}
else {
$message = $msg;
}
$success = mail($to, $subject, $message, $headers);
未上传文件时,消息会到达并带有换行符。但是,当附加文件时,它会崩溃。为什么?任何帮助表示赞赏!否则,该脚本将起作用。
看起来您是通过使用告诉电子邮件客户端这是HTML电子邮件Content-Type: text/html
。大多数电子邮件客户端会在纯文本版本上显示电子邮件的HTML内容。那意味着\r\n
不会工作。您需要<br />
在HTML版本中使用以获得换行符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句