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


I have a PHP script that sends an HTML email with an attached image. It works extremely well, however, in the email body, I & lt; Img & gt; Attachment can not be found to display in the tag Attached file is called postcard.png and the original file name on the server is 4e60348f83f2f.png . I have tried to give the image as different things: cid: postcard.png , cid: 4e60348f83f2f.png , postcard.png , And 4e60348f83f2f.png . nothing works.

I think the important part is that I am doing wrong here, because it creates a separate attachment instead of an inline attachment that I can use:

 < Code> Content-Transfer-Encoding: Base64 Content-Dispute: Attachment; Filename = "$ fname" // i.e.: "postcard.png"   

I tried to change it to use CID but I do not know how to do it , And this 'absolutely not work':

  Content-Transfer-Encoding: base64 Content-ID: & lt; $ Fname & gt; // Eye: postcard.   

Here's the full code: (This is based on a comment on PHP Mail page.)

  & lt ;; Php $ to = "recipient@email.com"; $ Email = "sender@email.com"; $ Name = "name"; $ Theme = "an inline image!"; $ Comment = "Lookout at Lt. Beed"; Lille  This  <48> LL  and Lord!   Img src = 'cid: postcard.png' & gt; & lt; br & gt; & lt; img src = 'cid: 4e60348f83f2f.png' & gt; & lt; img src = 'postcard.png' & gt; & Lt; br & gt; & lt; IMG src = '4e60348f83f2f.png' & gt; "; $ To = bar_tag ($ to); $ Text message = strip_tags (nl2br ($ comment), "& lt; br & gt;"); $ HTMLMessage = nl2br ($ comment); $ Sname = strip_tags ($ name); By $ email = strip_tags ($ email); $ Theme = strip_tag (theme $); $ Range 1 = rand (0, 9) "-" .Rend (10000000000, 99 99 99 99 99) "-". RAND (10000000000, 999999 99 99) "=:" .Road (1000099999); $ Range2 = rand (0, 9) "-". Rand (100000000, 99 99 99 99 99). "-" Rand (100000000, 99 99 99 99 99). "=:". Road (100009999); $ Filename1 = "4e60348f83f2f.png"; // file name on server with $ handle = fopen ($ filename1, 'rb'); $ F_contents = fread ($ handle, files ($ filename1)); $ Attachment = chunk_split (base64_encode ($ f_contents)); Fclose ($ handle); $ Ftype = "Image / PNG"; $ Fname = "postcard.png"; // What file will be named $ attachments = ''; $ Headers = & lt; & Lt; & Lt; From AKAM: $ fromName & lt; $ FromEmail & gt; Reply-to: $ Email MIME-Edition: 1.0 Content-Type: Multipart / Mixed; Border = "$ border1" AKAM; $ Attachments = & Lt; & Lt; & Lt; ATTA - $ range1 content-type: $ ftype; Name = "$ fname" content-transfer-encoding: base64 content-split: attachment; File name = "$ fname" $ ​​attachment ATTA; $ Body = & lt; & Lt; & Lt; AKAM is a multi-part message in this MIME format - $ limit1 content-type: multipart / optional; Border = "$ border2" - $ range2 content-type: text / plain; Charset = "windows-1256" content-transfer-encoding: quoted-printable $ text message - $ border2 content-type: text / html; Charset = "windows-1256" content-transfer-encoding: quoted-printable $ HTMLMessage - $ border2 - $ attachment - $ border1 - AKAM; // Send Email $ OK = Mail ($ per, $ theme, $ body, $ headers); Okay okay? "& Lt; h1> mail sent! ": "

mail was not sent! "; ? & Gt; I got the answer in the end, which has become remarkably simple. That helps me understand it, but I will display those parts that I needed to lower it.

First, the boundary string is created, and the image, correctly encoded and selected:

  // Create a border string. This requires unique (not in the text) ... // We are going to use sha1 algorithms to generate a 40 character string: $ sep = sha1 (date ('r', time ()); // Still prepare our inline image - Read, encode, split: $ inline = chunk_split (base64_encode (file_get_contents ('figure.gif')));   

In the HTML section of the email, the image is referred to this way (using the boundary string):

    

Then you make another part of the email under the HTML section for the inline attachment, such as:

  - PHP-related - {$ sep} Content-Type: Image / GIF Content-Transfer-Encoding: base64 Content-ID: & lt; PHP-CID - {$ sep} & gt; {$ Inline}   

... and that's it! Easy to implement PHPmailer or any other libraries, if it's all you are doing, no doubt for more complex tasks, you would like to get one of those libraries.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

c# - Confused over DLL entry points (entry point not found exception) -