PHPMailer and Inline Images

Been trying to find out how to send inline image attachments in a HTML email this afternoon, and my plodding eventually lead me to try PHPMailer after mucking about with some other class thing that didn’t do as I wanted.

After much stumbling about (a lot, considering I only wanted to do one thing) I came up with this:

<?
include(‘class.phpmailer.php’);

$mail=new phpmailer();
$mail->From =’sender@domain.com’;
$mail->FromName=’D';
$mail->Host =’localhost’;
$mail->Subject =’Testy testy test – YAY!’;
$mail->AddAddress(‘recipient@domain.com’);

$mail->IsHTML(true);
$mail->AddEmbeddedImage(‘bean.gif’,'beanimage’,'bean.gif’);
$mail->Body =’<HTML><BODY><IMG SRC=”cid:beanimage”></BODY></HTML>’;

$mail->Mailer =’mail’;
$mail->Send();
?>

Which seemed to do the job nicely. The end result is an email that contains an image but [a] doesn’t download it from a remote server and [b] doesn’t have an attachment. All this just to send out some spam promotional material!

2 Responses to “PHPMailer and Inline Images”

  1. Severin says:

    This is a very practical and useful example I have found. Thank you so much.

  2. Peminator says:

    It seems like webmails like roundcube do not display the inline in the message sent by phpmailer, while it displays ok in normal clients like thunderbird, macosxmail etc, but i have seen messages where the inline image displays ok even in this webmail client, so I believe there is something that could be made better in phpmailer to enhance the compatibility :-)
    /if somone has been fihting the same compatibility issue and found solution, please let me know/

Leave a Reply