Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AddEmbeddedImage() function embadding inline images as well as attaching same image as attachement

Tags:

html

php

email

I have added following parameters to PHPMailer object. Though I have embedded images for inline purpose using AddEmbeddedImage() function, it is working as expected, but additionally attaching same images as attachment to email & displaying at bottom.

$msg = `<table><tr><td colspan="2"><img  src="cid:header_jpg" alt="www.example.in" width="770" height="4" border="0" /></td></tr></table>`;

$mail = new PHPMailer(true); //New instance, with exceptions enabled
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth   = false;        // enable SMTP authentication
$mail->Port       = 25;           // set the SMTP server port
$mail->Host       = 'localhost';  // SMTP server
$mail->Username   = "";           // SMTP server username
$mail->Password   = "";           // SMTP server password

$mail->AddReplyTo($sender, $sender_name);

$mail->From       = $sender;
$mail->FromName   = $sender_name;

$mail->AddAddress($receiver);

$mail->Subject  = $subject;

//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 80; // set word wrap

$mail->MsgHTML($msg);

$mail->IsHTML(true); // send as HTML
$mail->AddEmbeddedImage('./images/header.jpg', 'header_jpg');          
$mail->AddEmbeddedImage('./images/logo.jpg', 'logo_jpg');        
$mail->AddEmbeddedImage('./images/alert_icon.png', 'alert_icon_png', 'alert_icon.png');        
$mail->Send();

Please suggest something as early as possible...

like image 546
Sumit Tawal Avatar asked Aug 26 '13 13:08

Sumit Tawal


1 Answers

I am having the same issue with web email embedded images. I tried different approaches and got these results:

Sending html email to Yahoo:

$mail->AddEmbeddedImage("some_picture.png", "my-attach", "some_picture.png", "base64", "image/png");

OR

$mail->AddEmbeddedImage("some_picture.png", "my-attach", "some_picture.png", "base64", "application/octet-stream");

OR

$mail->AddEmbeddedImage("some_picture.png", "my-attach", "some_picture.png");

Same results; Yahoo showed the embedded image correctly but still attached it too!

With hotmail, it correctly embedded the image and no attachments added.

Finally, I fount that PHPMailer has the ability to automatically embed images from your HTML email. You have to put the full path in the file system, when writing your HTML email. I ended up disregarding the AddEmbeddedImage and link the source of the image directly to its location on the website. It worked correctly in both Hotmail and Yahoo and no attachment was added in Yahoo.

<img src="http://FULL_PATH-TO-IMAGE" alt="THIS IS THE IMAGE" />

Needless to say, that embedded images in emails may not immediately show unless the user clicks the "show images" button; it all depends on their privacy & security settings.

I hope this helps!

like image 177
Rob Avatar answered Oct 14 '22 13:10

Rob