Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Embedding images into HTML-emails as attachments

by LanX (Saint)
on Mar 04, 2016 at 12:38 UTC ( [id://1156803]=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

Brethren!

For my current project I'm supposed to send HTML emails (sigh) with pictures (double-sigh) within a large company with a self made mailing module (triple-sigh)

Uploading the images to a webserver is no option since some of the recipients are outside the intranet.

At the moment we are using an self made mail module and producing multi-part emails where the images are inlined attachments referenced either by content-location or content-id .

see http://www.faqs.org/rfcs/rfc2557.html

Is there a generic module to do facilitate this?

I'd like to have a HTML template with placeholders for images which are automatically mapped to attachments to simplify the generation of those emails:

Something like <img src="{{ATTACH:path}}"> expanded to <img src='cid:3__=398519209581029819@sender@foo.org"> and the attachment with Content-Disposition: inline; and correct CID automatically appended to the mail.

--ibGD9CHXGX4XX Content-Type: application/octet-stream Content-Disposition: inline; filename=trains_footer.png Content-Transfer-Encoding: base64 Content-ID: 3__=398519209581029819@sender@foo.org iVBORw0KGgoAAAANSUhEUgAAAbsAAAB6CAYAAAAmqv27AAAAAXNSR0ICQMB9xQAAAAlwSF +lzAAAX EgAAFxIBZ5/SUgAAABl0RVh0U29mdHdhcmUATWljcm9zb2Z0IE9mZmljZX/tNXEAAKGSSU +RBVHja yadda yadda yadda

Actually I already re-inveted the wheel , but would be thankful to know about saner alternatives.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

UPDATES
Typo: s/attachement/attachment/ #thanks chacham

Replies are listed 'Best First'.
Re: Embedding images into HTML-emails as attachements
by duelafn (Parson) on Mar 04, 2016 at 13:26 UTC

    If you are willing to look past the warnings in the MIME::Lite documentation, MIME::Lite::HTML will read an HTML file, get/include the images, and transform the src attributes automatically. From documentation:

    • Get the file (LWP) if needed
    • Parse page to find include images (gif, jpg, flash)
    • Attach them to mail with adequat header if asked (default)
    • Include external CSS,Javascript file
    • Replace relative url with absolute one
    • Build the final MIME-Lite object with each part found

    I've usually paired it with HTML::FormatText::WithLinks to generate a text version as well

    Update: Looks like Email::MIME::CreateHTML is a modern replacement.

    Good Day,
        Dean

      Thanks a lot, looks great! I'll check this out! =)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: Embedding images into HTML-emails as attachements
by Corion (Patriarch) on Mar 04, 2016 at 12:47 UTC

    I've used Feeder::HelperRole::Mail from dakkars RSS-to-mail program to send me HTML pages with pictures in them. But that module is GPLv3, so taking it verbatim would make your whole program GPLv3, which is inconvenient.

    Maybe you can ask dakkar to give you a version of sub entry_to_mime that has a more liberal license. In the end, the module does a second pass over already rendered HTML and replaces all <img src="..."> by fetching the referenced URLs and then replacing the tags by <img src="cid:..." and appending the images to the MIME body.

Re: Embedding images into HTML-emails as attachements
by Your Mother (Archbishop) on Mar 04, 2016 at 13:13 UTC

    Probably not a good idea but there is also–

    printf '<img src="data:%s;base64,%s"/>', $img_mime_type, encode_base64($img_content);
      yeah I had the same idea, but googlemail's webclient is deleting those data entries from the HTML (at least in my tests).

      (didn't mention it in the OP to keep it short and people interested ;)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: Embedding images into HTML-emails as attachments
by perlfan (Vicar) on Mar 04, 2016 at 16:49 UTC
    Using the solution I mention in Re: Sending email with both html and plaintext, I have found that the resulting emails will display the images referenced using the <img> tag inline as expected and included as attachments.

    I only tested this in gmail, yahoo, and hotmail - so I am not sure if it's something they're doing or if the method I use first downloads the images and attaches them when sending. I should probably look into that =)

      Thanks, I'll check all suggestions at work during the week! =)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1156803]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-29 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found