Send email with PHPMailer - embed image in body
Solution 1
I found the answer:
$mail->AddEmbeddedImage('img/2u_cs_mini.jpg', 'logo_2u');
and on the <img>
tag put src='cid:logo_2u'
Solution 2
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");
Related videos on Youtube
elvispt
Full Stack Developer with experience with: PHP | Javascript | Typescript | jQuery | MySQL/MariaDB | Laravel/Lumen |CodeIgniter
Updated on July 08, 2022Comments
-
elvispt almost 2 years
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.
-
mtk over 11 yearsThat perfectly helps !!! Out of curiosity where did you find this? Please explain.
-
Plummer about 11 yearsWhy the backslash at the end of the cid?
-
elvispt about 11 yearsI 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.
-
Cruel almost 11 yearsI 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. -
PhoneixS almost 10 yearsFor 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).
-
Wolfgang Blessen over 3 yearsI was updating PHPMailer from 5.2 to 6.1 and unfortunatly my old solution with Inline embedded Images with <img src="data:image/png;base64...> doesnot work anymore. Your given solution means to add images by source, rather than by HTML Template, which makes it difficult to handle for me. Any Explanations or solutions, why inline data: doesnot work anymore?