我试图通过电子邮件发送pdf文件。我的附件文件已成功发送,但无法显示消息内容 (mainmessage =嗨,这是文件。) 。如果我使用phpmailer,则无法传递文件作为附件,但可以显示邮件内容。这是代码:
require("generate_pdf.php");
// Settings
$name = "John";
$email = "john@gmail.com";
$to = "$name <$email>";
$from = "Server@gmail.com";
$subject = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$fileatt = $path_file;
$fileatttype = "application/pdf";
$fileattname = $nama_file;
$headers = "From: $from";
// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers. = "\nMIME-Version: 1.0\n".
"Content-Type: multipart/mixed;\n".
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n".
"-{$mime_boundary}\n".
"Content-Type: text/plain; charset=\"iso-8859-1\n".
"Content-Transfer-Encoding: 7bit\n\n".
$mainMessage.
"\n\n";
$data = chunk_split(base64_encode($data));
$message. = "--{$mime_boundary}\n".
"Content-Type: {$fileatttype};\n".
" name=\"{$fileattname}\"\n".
"Content-Disposition: attachment;\n".
" filename=\"{$fileattname}\"\n".
"Content-Transfer-Encoding: base64\n\n".
$data.
"\n\n".
"-{$mime_boundary}-\n";
// Send the email
if (mail($to, $subject, $message, $headers))
答案 0 :(得分:0)
在身体部位之前只有一个连字符,因此不会显示
规范也说CRLF,所以你应该使用\r\n
alwayw