110

I'm trying to send HTML mail, with PHPMailer, with images. The body is loaded from a html file, that contains all the info.

When sending the mail, the image does not appear in the body, although I even send the image also as an attachment.

HTML <img> tag points to the same place as the place.

PHP:

$mail->AddAttachment('img/2u_cs_mini.jpg');

How can I make the html point to the attachment so the image can be loaded in the body.

Looking at the example that comes with PHPMailer I do not notice any difference, and in their case the image does appear.

showdev
  • 28,454
  • 37
  • 55
  • 73
elvispt
  • 4,832
  • 7
  • 27
  • 35

2 Answers2

240

I found the answer:

$mail->AddEmbeddedImage('img/2u_cs_mini.jpg', 'logo_2u');

and on the <img> tag put src='cid:logo_2u'

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
elvispt
  • 4,832
  • 7
  • 27
  • 35
  • 3
    That perfectly helps !!! Out of curiosity where did you find this? Please explain. – mtk Sep 22 '12 at 20:47
  • Why the backslash at the end of the cid? – Plummer Mar 12 '13 at 14:20
  • I did this so long ago, I don't remember if there was even a reason. I don't think there is. Just some garbage, most likely. – elvispt Mar 12 '13 at 20:55
  • 8
    I believe the backslash was mistakenly left there from a previous code that escaped quotes, like `echo "src=\"cid:logo_2u\""` and I don't believe the backslash is necessary. – Cruel May 17 '13 at 22:15
  • 10
    For who need an explanation from a non expert: you need to tell the rendering engine from where to get the image and with what protocol (to use an image that is attached you use de cid "protocol, Content-ID). And you use AddEmbeddedImage to give an id to the attachment so you can refer to it (Note that it can be sounds and other types of files). – PhoneixS Jul 02 '14 at 08:56
  • I was updating PHPMailer from 5.2 to 6.1 and unfortunatly my old solution with Inline embedded Images with – Wolfgang Blessen Sep 12 '20 at 07:17
6

According to PHPMailer Manual, full answer would be :

$mail->AddEmbeddedImage(filename, cid, name);
//Example
$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg '); 

Use Case :

$mail->AddEmbeddedImage("rocks.png", "my-attach", "rocks.png");
$mail->Body = 'Embedded Image: <img alt="PHPMailer" src="cid:my-attach"> Here is an image!';

If you want to display an image with a remote URL :

$mail->addStringAttachment(file_get_contents("url"), "filename");