How to send HTML email with inline attached image using PHP

I have a PHP script that sends an HTML email with an attached image. This works great, but I can’t get the application to appear in the tag <img>in the body of the message. The attached file is called postcard.png, and the original file name on the server is 4e60348f83f2f.png. I have tried to indicate URL-address of the image as a different thing: cid:postcard.png, cid:4e60348f83f2f.png, postcard.pngand 4e60348f83f2f.png. Nothing works.

I think the key part that I am doing wrong is here because it makes it a split attachment instead of the inline application that I can use:

Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="$fname" // i.e.: "postcard.png"

I tried changing it to use CID, but I really don't know how to do this, and this did not work at all:

Content-Transfer-Encoding: base64
Content-ID: <$fname> // i.e.: postcard.png

: ( php mail().)

<?php
$to      = "recipient@email.com";
$email   = "sender@email.com";
$name    = "Namename";
$subject = "An inline image!"; 
$comment = "Llookout <b>Llary</b> it <br> the <b>Ll</b>andllord!<br><img src='cid:postcard.png'><br><img src='cid:4e60348f83f2f.png'><img src='postcard.png'><br><img src='4e60348f83f2f.png'>";

$To          = strip_tags($to);
$TextMessage =strip_tags(nl2br($comment),"<br>");
$HTMLMessage =nl2br($comment);
$FromName    =strip_tags($name);
$FromEmail   =strip_tags($email);
$Subject     =strip_tags($subject);

$boundary1   =rand(0,9)."-"
    .rand(10000000000,9999999999)."-"
    .rand(10000000000,9999999999)."=:"
    .rand(10000,99999);
$boundary2   =rand(0,9)."-".rand(10000000000,9999999999)."-"
    .rand(10000000000,9999999999)."=:"
    .rand(10000,99999);

$filename1 = "4e60348f83f2f.png"; //name of file on server with script
$handle      =fopen($filename1, 'rb'); 
$f_contents  =fread($handle, filesize($filename1)); 
$attachment=chunk_split(base64_encode($f_contents));
fclose($handle); 

$ftype       ="image/png";
$fname       ="postcard.png"; //what the file will be named


$attachments='';
$Headers     =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="$boundary1"
AKAM;

$attachments.=<<<ATTA
--$boundary1
Content-Type: $ftype;
    name="$fname"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="$fname"


$attachment

ATTA;


$Body        =<<<AKAM
This is a multi-part message in MIME format.

--$boundary1
Content-Type: multipart/alternative;
    boundary="$boundary2"

--$boundary2
Content-Type: text/plain;
    charset="windows-1256"
Content-Transfer-Encoding: quoted-printable

$TextMessage
--$boundary2
Content-Type: text/html;
    charset="windows-1256"
Content-Transfer-Encoding: quoted-printable

$HTMLMessage

--$boundary2--

$attachments
--$boundary1--
AKAM;

// Send email
$ok=mail($To, $Subject, $Body, $Headers);
echo $ok?"<h1> Mail sent!</h1>":"<h1> Mail not sent!</h1>";
?>
+5
2

- , . , , , .

-, , :

// 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')));

HTML- ( ):

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

HTML , :

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

... ! , PHPmailer , , . , .

+14

PHPMailer, , , .

URL- img, HTML-, .

<img src="http://example.com/image.jpg"/>
0

All Articles