Search code examples
phpmailer

PHPMailer Embedded Image from attachment


I am the outsider for using script.

I am trying use AddEmbeddedImage and cid: to embed image from attachment, is that correct?? But failure...

Thank you very much for your help in advance !!!

$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());

Solution

  • You are calling uniqid() multiple times, and as the docs say, each time it will give you a new, random result, so your attachment CIDs will never match what's in your message content. Do it like this to ensure they match:

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