14

I am using phphmailer and have attached an image, it shows only the image like an icon rather than image itself here is my code could you help.

$mail->AddEmbeddedImage('2.jpg', '2img', '2.jpg');
$mail->Subject  =  "Order Form:  Contact form submitted";
$mail->Body     =  $body . 'img src="../../photo/2img" ;

note: i have dropped html tag befor img as I get error sending this Q.

Darren
  • 12,786
  • 3
  • 34
  • 71
Mary
  • 395
  • 4
  • 8
  • 17

2 Answers2

22

Per PHPMailer Manual, you sould use the method AddEmbeddedImage

$mail->AddEmbeddedImage(filename, cid, name);
By using this function with this example's value above, results in this code:
$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg '); 

like this:

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

so cid:my-attach will be replaced with the inline-source of the image that's inside the email body

Danny Beckett
  • 18,294
  • 21
  • 100
  • 129
Gabriel Sosa
  • 7,706
  • 3
  • 36
  • 48
  • Hello, this works great for embedding in the email, thank you. But doing this the image is not showing as a normal attachment at the bottom of the mail. Is there a way to both embed the image in the body of the mail and show it as an ordinary attachment at the bottom? Just so if the image fails to load, the receiver can still find it at the bottom. Thanks – Jakob Oct 21 '13 at 08:20
  • 1
    that will depend completely on the email client. At the end, that image it's just an attachment. – Gabriel Sosa Oct 21 '13 at 17:49
  • huh, okay. Thanks very much – Jakob Oct 21 '13 at 19:01
  • what if i have to provide a redirect Url to an Image in the same context – shubh14896 Jun 02 '17 at 08:05
  • just wrap the `img` tag with a `a` like you would usually do. that doesn't change the way the image is treated. – Gabriel Sosa Jun 03 '17 at 10:08
  • To embed an image in the body of the email I only need 2 parameters `$mail->AddEmbeddedImage(/path/to/file/, "photo");`and then in the body of the message `` – dstonek Jan 07 '19 at 12:16
-1

Using the AddEmbeddedImage() function works well in showing the embedded image in the web based emails. However, Yahoo always adds it as an attachment too. To overcome this problem, you can safely disregard the AddEmbeddedImage() and link to the full path of the image on your server and PHPMailer has the capacity of converting it to CID and It will correctly show as embedded image and Yahoo won't add it as an attachment anymore.

In the html body of the message, add it like usual:

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

Rob
  • 109
  • 2
  • 3
    Jus to be clear, to anyone who's reading this, phpmailer is not converting this to cid or any other type of embedded image. it isn't parsing it at all, if you see the image on the other side, it's juste because of the basic html img tag allowing you to specify a full url to the image. But be aware, as this technique is used to track delivery (beacon) some spam software doesn't likes this, and most mail apps won't load remote images (that how you call them ) automatically, user will have to press some sort of link like 'show images' – Vincent Duprez Oct 28 '14 at 14:22