PHP mail()附件已损坏

Dre*_*rew 1 php email

我一直在努力尝试使用PHP发送带附件的电子邮件.它曾经工作,但邮件正在被打乱.现在我已经让消息体工作,但附件会破坏.我以前使用base64编码的消息体,但现在使用7位.谁能告诉我我做错了什么?

PS请不要告诉我,我应该使用预先制作的课程来做到这一点.我尝试了几个,但都没有成功.如果我不克服这些问题,我将永远不会学会如何正确地做到这一点.谢谢

//define the receiver of the email
$to = 'a@something.co.uk';
//define the subject of the email
$subject = 'Your Disneyland Paris entry';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \n
$mime_boundary = "<<<--==+X[".md5(time())."]";

$path = $_SERVER['DOCUMENT_ROOT'].'/two/php/';
$fileContent =  chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf')));


$headers .= "From: info@blah.org.uk <info@blah.org.uk>"."\n";

    $headers .= "MIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed;\n" .
            " boundary=\"{$mime_boundary}\"";   

$message = "This is a multi-part message in MIME format.\n";

$message .= "\n";
$message .= "--".$mime_boundary."\n";

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n";
$message .= "\n";
$message .= "messagebody \n";
$message .= "--".$mime_boundary."" . "\n";

$message .= "Content-Type: application/octet-stream;\n";
$message .= " name=\"CTF-brochure.pdf\"" . "\n";
$message .= "Content-Transfer-Encoding: 7bit \n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"CTF_brochure.pdf\"\n";
$message .= "\n";
$message .= $fileContent;
$message .= "\n";
$message .= "--".$mime_boundary."--\n";

//send the email
$mail_sent = mail($to, $subject, $message, $headers);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
Run Code Online (Sandbox Code Playgroud)

Jos*_*osh 7

我可能错了,但我相信你必须以某种方式对PDF进行编码,7bit将无法正常工作,因为PDF文件的内容超出了范围.为什么不在PDF中使用base64?