Send email with PHPMailer - embed image in body
I found the answer:
$mail->AddEmbeddedImage('img/2u_cs_mini.jpg', 'logo_2u');
and on the <img>
tag put src='cid:logo_2u'
How to embed image in phpmailer - I can't do it, why?
Add on the <img>
tag put src='cid:Kartka'
$mail->AddEmbeddedImage('images/Kartka.png', 'Kartka');
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
"<p>This is a test picture: <img src=\"cid:Kartka\" /></p>";
Why you use so much \ ?? you can do it also like this :
<img src="cid:Kartka"/>
Adding embedded images within mail body phpmailer class
Don't use $mailer->AddEmbeddedImage
, but directly add
<img src="http://.../images/namDiams.png" />
instead.
The mail length should be lighter... And it works.
EDIT
I don't know if it will help you but there is a little mistake here :
$mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDimes.png');
Should be
$mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDiames.png');//the last param the second 'a' was missing...
Another topic here
Embed Image in Mail without having physical Image File
It always helps to read the docs. The function you're looking for is addStringEmbeddedImage
, which does exactly what you want:
$URL = "http://maps.googleapis.com/maps/api/staticmap?center=Albany,+NY&zoom=13&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true";
$image = file_get_contents($URL);
$mail->addStringEmbeddedImage($image, 'staticMap', 'map.png', 'base64', 'image/png');
$mail->Body = '<img width="600" height="300" src="cid:staticMap">';
There are indeed pros and cons to using embedded images, but data urls have especially poor client compatibility, so are generally the worst option. Dynamically linking to the image may not be reliable with google maps, so you may want to proxy and cache the direct map lookups. This will also be much faster than hitting the API every time, and also more reliable as you can deliver an older version if the API doesn't respond or is slow.
How can I embed an image into an email acceptable to Outlook
Most email clients do not support Base 64 images in the HTML. (See https://www.caniemail.com/features/image-base64/)
However, you can add your image as a CID embedded attachment to your email, and then link to that in your HTML email.
Here’s another Stack Overflow thread answering this question : How to embed images in html email
What is the most common way to send email that contains an image from a database via phpMailer?
The solution that worked for me using an image that was encoded as base64.
$image_data = explode(',', $_SESSION['icon_data']);
$image_data = base64_decode($image_data[1]);
$icon = $image_data;
Before, I just tried to decode the whole column cell; I didn't realize there was a comma in the data that I needed to split using explode, and only use the second index which was the actual base64 data.
from here, I just used the addStringEmbeddedImage:
$w_mail->addStringEmbeddedImage($icon, 'img', '', 'base64', "image/png");
Works for now. Thank you all for the feedback and the help
Related Topics
Calling C/C++ Library Function from PHP
How to Use Cookies Across Two Different Domains
Setting Value of a HTML Form Textarea
Passing PHP Variable from Controller to JavaScript Function in the Head of a View
Post Content-Length Exceeds the Limit
Bounce Email Handling with PHP
Resize Images in PHP Without Using Third-Party Libraries
How to Pass Variables Received in Get String Through a PHP Header Redirect
Mysqli Error: User Already Has More Than 'Max_User_Connections' Active Connections
How to Modify Xml File Using PHP
Print Time in 15-Minute Increments Between Two Times in the Same Day
How to Capture the Result of Var_Dump to a String
Passing Data to Buildform() in Symfony 2.8, 3.0 and Above
Dyld: Library Not Loaded: /Usr/Local/Lib/Libpng16.16.Dylib with Anything PHP Related