I am sending email using PHPmailer and tying to embed image in mail body (using CID method <img src="cid:qrcode" />
) but it always attaching image instead of embedding.Can anyone tell me what's wrong with my code (commented lines in code, already tried. ).
-
1Please don't post code as images. It means it's not searchable, editable, or copyable for anyone wanting to show you amendments. – Synchro Jan 15 '19 at 09:41
3 Answers
First of all you're using a very old version of PHPMailer, and have based your code on a very old example. Get the latest.
The other obvious problem is that while you're putting HTML into Body
, you've commented out the call to isHTML()
, so your message is being sent as plain text, which has no concept of displaying images inline. Uncomment this line:
$mail->isHTML();
Also bear in mind that in MIME there is essentially no difference between attachments and inline images - everything is an attachment, it's just that some attachments may be referred to from HTML parts, and HTML-capable clients can make use of that linkage.
If Outlook is removing src
attributes, that's clearly not your sending code's problem. Outlook does some very unpleasant things to email.
One other minor thing: instead of dirname(__FILE__)
you can use __DIR__
in any current version of PHP.

- 35,538
- 15
- 81
- 104
just as an assistance I encountered the same problem and after much mumbling arrived at a solution which might help others.
- Tried dumping the image in the same folder - nope
- Tried DIR variable to dynamicalyly pull in the image - nope
Finally, hardcoded the path to the folder in which my embedded image lived, hurrah
Magic forumla for me (using PHPmailer v5.5) - note I'm using Plesk so your definitive path may differ, use mine as a guide...
$mail->AddEmbeddedImage("/var/www/vhosts/{domainname}/httpdocs/{foldername)/image.jpg", "emailimg", "image.jpg");
I note that when calling in the image as an embedded image that i had to use the same filename as I think PHPmailer uses the structure:
embedded-img-name source, reference id, embedded-img-name within the internal AddEmbeddedImage call
Hope it helps someone!

- 11
- 3
Try this code i think it will work for you
$mail->AddEmbeddedImage('img/2u_cs_mini.jpg', 'logo_2u');
and on the tag put src='cid:logo_2u'

- 507
- 1
- 3
- 13
-
You can add this code under your body tag, its work i am already using this – Harsh Khare Jan 15 '19 at 07:15
-
I just used these two line ` $mail->Body = "Embedded Image src='cid:logo_2u' "; $mail->AddEmbeddedImage(dirname(__FILE__).'/footer.jpeg', 'logo_2u'); ` and same issue. – Irphan Jan 15 '19 at 07:18
-
Try this https://stackoverflow.com/questions/15518703/adding-embedded-images-within-mail-body-phpmailer-class – Harsh Khare Jan 15 '19 at 07:20
-
1I already checked & tried this post. if we use img tag, gmail or outlook remove src .I have also tried by chaging content type , adding headers etc but nothing working. image is getting attached always. – Irphan Jan 15 '19 at 07:25
-
`$mail = new PHPMailer(true); $mail->isSMTP(); $mail->Host = 'localhost'; $mail->SMTPAuth = false; $mail->setFrom('test@mytest.com', 'Chris1111'); $mail->addAddress('test@test.com'); $mail->isHTML(true); $mail->AddEmbeddedImage('footer.jpeg', 'logoimg'); $mail->Body = "
Test 1 of phpmailer
This is test Picture:
"; $mail->AltBody = "This is the text only alternative body"; $mail->send(); echo 'Message has been sent!';` – Irphan Jan 15 '19 at 07:36 -