这个php脚本可以通过电子邮件发送我的pdf文件。
问题是脚本没有发送$ mainMessage中指定的任何消息。
为什么会出现这个问题,脚本只发送没有任何消息的pdf文件?
// Settings
$name = "Name";
$email = "someome@anadress.com";
$to = "$name <$email>";
$from = "email@email.com";
$subject = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$fileatt = "test.pdf";
$fileatttype = "application/pdf";
$fileattname = "newname.pdf";
$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)) {
echo "The email was sent.";
}
else {
echo "There was an error sending the mail.";
}
答案 0 :(得分:0)
尝试使用\r\n
代替\n
。此外,我建议您使用库来发送电子邮件,例如PHPMailer或SwiftMailer。
编辑:当您声明自己的字符集时("
- 它输出"Content-Type: text/plain; charset=\"iso-8859-1\n"
),似乎会有额外(或缺失)Content-Type: text/plain; charset="iso-8859-1
。< / p>
答案 1 :(得分:0)
你没注意到你需要完成这一行:
$message = "This is a multi-part message in MIME format.\r\n" .
"--{$mime_boundary}\r\n" .
"Content-Type: text/plain; charset=utf-8 \r\n" .
"Content-Transfer-Encoding: 7bit\r\n" .
$mainMessage . "\r\n\r\n";
你在“ - {$ mime_boundary} \ r \ n”中遗漏了一个“ - ”。 它应该像我写的那样:3