php 发送带附件的邮件,php发送带附件的电子邮件
我似乎無法找到這個我寫的php函數的問題,應該發送帶附件的電子郵件.我已經掙扎了很長一段時間.
function myMail($to, $subject, $mail_msg, $filename, $contentType){
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@example.com\r\nReply-To: ".$to;
$headers .= "\r\nContent-Type: ".$contentType.
"; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
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: text/plain; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--
";
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );
return $mail_sent ? "Mail sent" : "Mail failed";
}
編輯問題是郵件的消息與文件混合并作為附件發送.
解決方法:
我剛剛查看了幾封電子郵件,并注意到最終的附件邊界以’ – ‘結尾,而開放邊界標記則沒有.在您的代碼中,您有:
--PHP-mixed-$random_hash--
Content-Type: text/plain; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--
也許它應該是:
--PHP-mixed-$random_hash
Content-Type: text/plain; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--
看看這里的例子:
標簽:php,email,attachment
來源: https://codeday.me/bug/20190916/1807848.html
總結
以上是生活随笔為你收集整理的php 发送带附件的邮件,php发送带附件的电子邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php js 异步上传图片,javasc
- 下一篇: php$后面加点有什么用,css和js后