Php 将pdf附加到电子邮件。消息不';行不通

Php 将pdf附加到电子邮件。消息不';行不通,php,pdf,sendmail,Php,Pdf,Sendmail,这个php脚本可以通过电子邮件发送我的pdf文件 问题是脚本没有发送$mainMessage中指定的任何消息 为什么会出现这样的问题,脚本只发送pdf文件而不发送任何消息 // Settings $name = "Name"; $email = "someome@anadress.com"; $to = "$name <$email>"; $from = "email@email.com";

这个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.";
    }
//设置
$name=“name”;
$email=”someome@anadress.com";
$to=“$name”;
$from=”email@email.com";
$subject=“这是您的附件”;
$mainMessage=“嗨,这是文件。”;
$fileatt=“test.pdf”;
$fileatttype=“应用程序/pdf”;
$fileattname=“newname.pdf”;
$headers=“From:$From”;
//文件
$file=fopen($fileatt,'rb');
$data=fread($file,filesize($fileatt));
fclose($文件);
//这将附加文件
$semi_rand=md5(time());
$mime_boundary=“==Multipart_boundary_x{$semi_rand}x”;
$headers.=“\n时间版本:1.0\n”。
“内容类型:多部分/混合;\n”。
“边界=\”{$mime\u boundary}\”;
$message=“这是MIME格式的多部分消息。\n\n”。
“-{$mime_boundary}\n”。
“内容类型:文本/普通;字符集=\”iso-8859-1\n“。
“内容传输编码:7bit\n\n”。
$mainMessage。“\n\n”;
$data=chunk_split(base64_encode($data));
$message.=“--{$mime\u boundary}\n”。
“内容类型:{$fileatttype}\n”。
“名称=\”{$fileattname}\“\n”。
“内容处置:附件\n”。
“文件名=\”{$fileattname}\“\n”。
“内容传输编码:base64\n\n”。
$data。“\n\n”。
“-{$mime_boundary}-\n”;
//发送电子邮件
if(邮件($to、$subject、$message、$headers)){
回显“电子邮件已发送。”;
}
否则{
echo“发送邮件时出错。”;
}

尝试使用
\r\n
而不是您的
\n
s。此外,我建议使用库发送电子邮件,例如或


编辑:当您声明您的字符集时(在
“内容类型:text/plain;charset=\“iso-8859-1\n”
-它输出
内容类型:text/plain;charset=“iso-8859-1
)。

您没有注意到您需要完成这一行:

 $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\u boundary}\r\n”中缺少一个“-”。
这应该是我写的例子:3

,不幸的是,这不起作用。你还有别的想法吗?