How to Send an HTML Email with an Inline Attached Image with PHP

how to send an HTML email with an inline attached image with PHP

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 a 40 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.

Sending email with inline image

Finally I come to know how to do this. Here is the source link from where I come to know


$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";

$headers = "From: youremail@host.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;

$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;

$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'

<html>

<head>
<style> p {color:green} </style>
</head>
<body>

A line above
<br>
<img src="cid:http://localhost/a/img/1.jpg">
<br>
a line below
</body>

</html>'."\n\n".
$bound;

$file = file_get_contents("http://localhost/a/img/1.jpg");

$message .= "Content-Type: image/jpeg; name=\"http://localhost/a/img/1.jpg\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-ID: <http://localhost/a/img/1.jpg>\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;

echo mail($to, $subject, $message, $headers) ;

?>

Send email with PHPMailer - embed image in body

I found the answer:

$mail->AddEmbeddedImage('img/2u_cs_mini.jpg', 'logo_2u');

and on the <img> tag put src='cid:logo_2u'

PHP Attaching an image to an email

Try the PEAR Mail_Mime package, which can embed images for you.

You need to use the addHTMLImage() method and pass a content id (cid), which is a unique string of text you will also use in your img's src attribute as a cid: URL. For example:

include('Mail.php');
include "Mail/mime.php";

$crlf = "\r\n";
$hdrs = array(
'From' => 'foo@bar.org',
'Subject' => 'Mail_mime test message'
);

$mime = new Mail_mime($crlf);

//attach our image with a unique content id
$cid="mycidstring";
$mime->addHTMLImage("/path/to/myimage.gif", "image/gif", "", true, $cid);

//now we can use the content id in our message
$html = '<html><body><img src="cid:'.$cid.'"></body></html>';
$text = 'Plain text version of email';

$mime->setTXTBody($text);
$mime->setHTMLBody($html);

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send('person@somewhere.org', $hdrs, $body);

How can I embed an image into an email acceptable to Outlook

Most email clients do not support Base 64 images in the HTML. (See https://www.caniemail.com/features/image-base64/)

However, you can add your image as a CID embedded attachment to your email, and then link to that in your HTML email.

Here’s another Stack Overflow thread answering this question : How to embed images in html email



Related Topics



Leave a reply



Submit