我正在尝试在 php 中实现邮件功能,它与单个附件一起工作正常,但问题是当我尝试发送多个附件时,它不起作用。我正在使用 php mail() 函数发送电子邮件,我正在尝试附加 PDF 和图像文件。如果附加PDF,则不会附加图像,如果附加图像,则不会附加PDF。有什么想法我做错了吗?
$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';
}