0
我面臨一個問題,即發送帶有Excel附件的PHP電子郵件。我可以看到郵件發送的消息,郵件即將到來,但沒有附件。我知道真正的問題。這是我的代碼。我已將User.xlsx文件放入根目錄中。請幫我解決這個問題。謝謝帶附件的PHP電子郵件
$to = "[email protected]";
$subject="attachement";
$mail_msg="message with attach";
$filename="User.xlsx"; // Attachement file in root directory
$contentType="application/zip"; // Not sure about what to put here
$pathToFilename="./";
$random_hash = md5(date('r', time()));
$headers = "From: [email protected]\r\nReply-To: ".$to;
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($pathToFilename)));
ob_start();
echo "
--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"
--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"utf-8\"
Content-Transfer-Encoding: 7bit
$mail_msg
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: $contentType; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--
";
$message = ob_get_clean();
// $fh=fopen('log.txt','w');
// fwrite($fh,$message);
$mail_sent = @mail($to, $subject, $message, $headers);
echo $mail_sent ? "Mail sent" : "Mail failed";
如果你想找出你的手動啞劇構建的問題,那麼做。在這裏研究數百個其他重複項。但不要求其他人進行調試。 PHPMailer和SwiftMailer都允許使用附件發送的簡單郵件,而無需擺弄從某處複製的代碼。 – mario