how to send an HTML email with an inline attached image with PHP -
i have php script sends html email attached image. works beauifully, however, can't attachment display in <img> tag in email body. attached file called postcard.png , original filename on server 4e60348f83f2f.png. i've tried giving image url various things: cid:postcard.png, cid:4e60348f83f2f.png, postcard.png, , 4e60348f83f2f.png. nothing works.
i think key part i'm doing wrong here, because makes separated attachment instead of inline attachment can use:
content-transfer-encoding: base64 content-disposition: attachment; filename="$fname" // i.e.: "postcard.png" i've tried changing use cid don't know how that, , didnt' work @ all:
content-transfer-encoding: base64 content-id: <$fname> // i.e.: postcard.png here's full code: (it's based on this code comment in php mail() page.)
<?php $to = "recipient@email.com"; $email = "sender@email.com"; $name = "namename"; $subject = "an inline image!"; $comment = "llookout <b>llary</b> it's <br> <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 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 file 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 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>"; ?>
i found answer, turns out remarkably simple. this page helped me figure out, i'll demonstrate parts needed done below.
first off, there's creation of boundary string, , image, correctly encoded , chunked:
// create boundary string. needs unique (not in text) ... // going use sha1 algorithm generate 40 character string: $sep = sha1(date('r', time())); // prepare our inline image - read, encode, split: $inline = chunk_split(base64_encode(file_get_contents('figure.gif'))); in html part of email, image referenced (using boundary string):
<img src="cid:php-cid-{$sep}"> then create part of email below html part inline attachment, this:
--php-related-{$sep} content-type: image/gif content-transfer-encoding: base64 content-id: <php-cid-{$sep}> {$inline} ...and that! easier implementing phpmailer or of other libraries, if you're doing. no doubt more complicated task, you'll want 1 of libraries.
Comments
Post a Comment