3

Im trying to embed an image within my message body but it ends up as an attachment

    $mailer->Subject = APP_NAME . " - " . $name . " send you and Ad : " . $row['name'];
    $mailer->IsHTML(true);
    $mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDimes.png'); 

    //footer
    $footer = "Regards<br/><br/>";
    $footer .= '<table style="width: 95%">';
    $footer .= '<tr>';
    $footer .= '<td>';
    $footer .= "<strong><span style='font-size: 15px'>NamDimes Team</span></strong><br/>
                    NamDimes<br/>
                    Contact Number: " . APP_CONTACT . "<br/>
                    Email: " . APP_EMAIL . "<br/>
                    Website: " . APP_WEBSITE . "<br/>";
    $footer .= '</td>';
    $footer .= '<td style="text-align:right">';
    $footer .= '<img src=\"cid:logoimg\" />';
    $footer .= '</td>';
    $footer .= '</tr>';
    $footer .= '</table>';

    $mailer->Body = $body . $footer;
    $mailer->AltBody="This is text only alternative body.";
    $mailer->AddAttachment('../' . $row['image_path'], $row['name'] . ".jpg");

i have set everything else, including the addresses, the mail gets send out, logo image that I want embed in the body gets attached as an attachment, anyone know why?

Lappies
  • 903
  • 2
  • 11
  • 29
  • 1
    You code seems to work. Be sure to have latest PHPmailer version. By the way, IMHO you should use external images and not embedded ones. – Ghigo Mar 20 '13 at 08:43
  • i dont get you on the externel images part, kind of new to this – Lappies Mar 20 '13 at 08:47
  • just plain `http://` links. Just like `http://your.site.com/img/mypic.png` – Ghigo Mar 20 '13 at 08:49
  • the src attribute is removed if i do this – Lappies Mar 20 '13 at 09:05
  • Embedded images vs external images are two completely different things – if you want to make sure the image gets displayed in as many mail clients as possible, you should stick with embedded images – because external images are highly likely to get blocked to avoid user tracking. – CBroe Mar 20 '13 at 09:13
  • And what about removing backslashes in the img src? like this `$footer .= '';` – JoDev Oct 16 '13 at 12:32

4 Answers4

5

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

jonas-l-b
  • 271
  • 1
  • 15
JoDev
  • 6,633
  • 1
  • 22
  • 37
2

I can confirm that user2189925's answer does work. However, I use the absolute path since the location of the calling script is more likely to change than the location of the image.
e.g.

<img src="C:\folder\images\namDiames.png" />
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
NathanW
  • 302
  • 2
  • 9
  • Pointing to a local file on your harddrive will never show up in the actual e-mail. So this will never work. –  Jun 07 '16 at 09:21
  • This worked for me. PHPMailer was smart enough to embed the image in the email by replacing the value of the `src` attribute with the actual source data of the image. – NathanW Jun 07 '16 at 16:46
1

Faced the same problem, then I decided to replace the following

<img src="img/example.jpg"

with

<img src= "https://mysitename.com/img/example.jpg">

and it worked.

Naveen
  • 769
  • 1
  • 9
  • 28
-3

just give path of your image to the mail body eg: (img src="../images/cat.jpeg) it will definately work

Sahil Bhatia
  • 165
  • 1
  • 1
  • 8