来自附件的 PHPMailer 嵌入式图像

PHPMailer Embedded Image from attachment

我是脚本外行

我正在尝试使用 AddEmbeddedImagecid: 从附件中嵌入图像,是否正确?但是失败...

非常感谢您的提前帮助!!!

$signature = $_POST['signature'];
$signatureFileName = uniqid().'.png';
$signature = str_replace('data:image/png;base64,', '', $signature);
$signature = str_replace(' ', '+', $signature);
$data = base64_decode($signature);
$file = 'signatures/'.$signatureFileName;
file_put_contents($file, $data);


$message='Subject:  '.$_POST['subject'].'<br /> 
<img src="cid:'.uniqid().'.png"/> <br />
<img src="cid:'.uniqid().'"/> <br />
';    

require "../phpmailer/class.phpmailer.php";
require "../phpmailer/setting.php";
$mail->Subject = "Image Form"; 
$mail->MsgHTML($message);
$mail->AddEmbeddedImage($file, uniqid());

您多次调用 uniqid(),并且 as the docs say 每次都会给您一个新的随机结果,因此您的附件 CID 永远不会与邮件内容中的内容匹配。这样做以确保它们匹配:

$cid = uniqid();
$message='Subject:  ' . $_POST['subject'] . '<br /> 
<img src="cid:' . $cid . '"/> <br />';
$mail->addEmbeddedAttachment($file, $cid);