Skip to content Skip to sidebar Skip to footer

How To Send An Html Email With An Inline Attached Image With Php

I have a PHP script which sends an HTML email with an attached image. It works beauifully, however, I can't get the attachment to display in an tag in the email body. T

Solution 1:

I finally found the answer, which turns out to be remarkably simple. This page is what helped me figure it out, but I'll demonstrate the parts that I needed to get it done below.

First off, there's the creation of the boundary string, and the image, correctly encoded and chunked:

// Create a boundary string.  It needs to be unique (not in the text) so ...
// We are going to use the sha1 algorithm to generate a40 character string:
$sep = sha1(date('r', time()));

// Also now prepare our inline image - Also read, encode, split:
$inline = chunk_split(base64_encode(file_get_contents('figure.gif')));

In the HTML part of the email, the image is referenced like this (using the boundary string):

<img src="cid:PHP-CID-{$sep}">

Then you create another part of the email below the HTML part for the inline attachment, like this:

--PHP-related-{$sep}
Content-Type: image/gif
Content-Transfer-Encoding: base64
Content-ID: <PHP-CID-{$sep}>
{$inline}

...and that is that! Easier than implementing PHPmailer or any of the other libraries, if this is all you're doing. No doubt for more complicated task, you'll want to get one of those libraries.

Solution 2:

I would recommend using PHPMailer but since you say you don't want to I suggest this quick fix.

Put the absolute URL inside an img tag, if you do send an HTML email, it will work.

<imgsrc="http://example.com/image.jpg"/>

Post a Comment for "How To Send An Html Email With An Inline Attached Image With Php"