我正在尝试在php中实现邮件功能,它可以通过单个附件正常工作,但是问题是当我尝试发送多个附件时,它无法正常工作。我正在使用php mail()函数发送电子邮件,我试图附加PDF和图像文件。如果附有PDF,则不会附加图片;如果附有PDF,则不会附加PDF。有什么想法我做错了吗?
I am trying to implement mail functionality in php, its working fine with single attachment, but problem is when I am trying to send more than one attachment, its not working. I am using php mail() function for sending emails, I am trying to attach a PDF and an Image file. If PDF attach then Image won't attach, if Image attach then PDF won't attach. Any thoughts where I'm doing wrong?
$header .= 'From: test <noreply@test.ae>' . "\r\n"; $header .= "MIME-Version: 1.0\r\n"; $file = '1.png' $displayname = '1.png'; $file_size = filesize($file); $handle = fopen($file, "r"); $content = fread($handle, $file_size); fclose($handle); $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $name = basename($file); $filepdf = '1.pdf' $displaynamepdf= '1.pdf'; $file_sizepdf = filesize($filepdf); $handlepdf = fopen($filepdf, "r"); $contentpdf = fread($handlepdf, $file_sizepdf); fclose($handlepdf); $contentpdf = chunk_split(base64_encode($contentpdf)); $name = basename($file); $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $header .= $message."\r\n\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-Type: application/octet-stream; name=\"".$displayname."\"\r\n"; // use different content types here $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$displayname."\"\r\n\r\n"; $header .= $content."\r\n\r\n"; $header .= "Content-Type: application/octet-stream; name=\"".$displaynamepdf."\"\r\n"; // use different contentpdf types here $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$displaynamepdf."\"\r\n\r\n"; $header .= $contentpdf."\r\n\r\n"; $header .= "--".$uid."--"; if (mail($to, $subject, "", $header)) { return 'sent'; } else { return 'not sent'; }推荐答案
PHPMailer 是电子邮件的最佳选择。
PHPMailer is the best to use for email.
签出一些下面的链接,这些链接将来也会对您有帮助:
checkout some below links which will help you in future also:
这可能会对您有所帮助。
may this help you.
更多推荐
php:电子邮件发送失败,并带有多个附件
发布评论