Embed Images in PHPMailer

I’ll keep this one short and fast:

PHPMailer has lots of features, even if it’s a bit old.

One great feature is that it allows you to embed images inline within your e-mail’s body. In most email clients, the inline embedded images are shown in their right place just as on a web page and are “also” in the attachements list.

To be able to use this feature, proceed as follows:

1- Instantiate PHPMailer:


$mail = new PHPMailer(true);
$mail->CharSet='UTF-8';
...
...
//Path to image file (use full path)
$filepath= '/path/to/file/logo_mail.jpg';
//File token ID, use unique interger.
$filecid = 1001;
//File name which will be in the mail, overrides default filename.
$filename= 'shop-logo.jpg';
//Encoding
$encodingtouse = 'base64';
//File type MIME
$filemime = 'image/jpeg';
//EMBED IT
$mail->AddEmbeddedImage($filepath, $filecid, $filename, $encoding, $filemime);
//Message Html Body
$mail->MsgHTML(file_get_contents('/path/to/contents.html'));

2- In your /path/to/contents.html file, which is your mail template message, you can include the inline image. Use this syntax for the <img> tag:

<img src=”cid:1001” alt=”shop logo used in emails” width=”182″ height=”75″ />

Note the CID here, it should be the same as the one you defined for your embedded inline image.

Tested and working with PHPMailer 5.1 on GMAIL, Yahoo!Mail and Mac Mail application

 

ref: http://www.sitepoint.com/forums/php-34/embed-images-phpmailer-586789.html

Join the Conversation

2 Comments

  1. Hello, I tried this procedure to send image via Phpmailer.
    But i got an error in email, I am using version 2.14.6
    Is this compatible or any other procedure….

  2. Thanks for posting this, it really helped.
    For some reason, the $filemime or $encoding, were killing the success for me. I removed those from the line and then it worked.
    Thanks again

Leave a comment

Your email address will not be published. Required fields are marked *