Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Need help generating an HTML mail with Email::MIME::Kit

by xMonk (Initiate)
on Sep 13, 2017 at 05:52 UTC ( #1199261=perlquestion: print w/replies, xml ) Need Help??
xMonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I've been working on a module for work and in one part of it I need to generate an email object using Email::MIME::Kit.

Here are my requirements:

  1. The email is going to be HTML formatted with images.
  2. It will sometimes contain a separate file attachment.
  3. I want the email to be generated from a scalar or hash containing the template.

I've been looking for docs/tutorials/anything to help me with this for > 1 day now and I'm so frustrated...

If anyone can help me with this, I'd be eternally thankful!

Here's my code for the sub so far:

sub create_mime_email { # Parse arguments my $template_path = shift or die q~No template file found in provide +d path~; my $manifest_reader = shift // 'JSON'; # Load required modules use Email::MIME::Kit; my $kit = Email::MIME::Kit->new({ source => $template_path, manifest_reader => $manifest_reader, }); my $email = $kit->assemble(); return $email; }

... And the template code:

{ "header": [ { "Subject": "MIME e-mail test" }, { "From": "John Doe <>" }, { "To": "Jane Doe <>" } ], "alternatives": [ { "container_type": "multipart/related", "type": "text/html", "path": "better-alternative.html", "attributes": { "charset": "utf-8" }, "attachments": [ { "type": "image/jpeg", "path": "logo.jpg" } ] } ], "attachments": [ { "type": "application/pdf", "path": "bogus-report.pdf", "attributes": { "filename": "report.pdf" } } ] }

... And the contents of the html template file:

<html> <body> Hello World! <img src='cid:[% cid_for("logo.jpg") %]' /> </body> </html>

Replies are listed 'Best First'.
Re: Need help generating an HTML mail with Email::MIME::Kit
by roboticus (Chancellor) on Sep 13, 2017 at 12:41 UTC


    What sort of difficulties are you having?


    When your only tool is a hammer, all problems look like your thumb.

      Well, I was having problems getting it to work in general.

      I got a bit further yesterday night and ended up with this template manifest.json-file which now takes care of requirements 1 and 2:

      { "renderer": "TestRenderer", "header": [ { "Subject": "MIME e-mail test" }, { "From": "John Doe <>" }, { "To": "Jane Doe <>" } ], "alternatives": [ { "attributes": { "content_type": "text/html", "charset": "utf-8" }, "body": "HTML goes <h3>HERE</h3><br>You can also use variables, +like this:<br>E.g. the variable friend = '[% friend %]' and gets rend +ered when the email object is created." } ], "attachments": [ { "type": "application/pdf", "path": "bogus-report.pdf", "attributes": { "filename": "report.pdf" } } ] }

      I still don't know how to load a scalar/hash when creating the "kit". This is how that part of the code is looking right now:

      my $kit = Email::MIME::Kit->new({ source => './dir/where/template/file/is', manifest_reader => 'JSON', }); my $email = $kit->assemble({ friend => 'xMonk', });
Re: Need help generating an HTML mail with Email::MIME::Kit
by xMonk (Initiate) on Sep 14, 2017 at 06:10 UTC

    Problem finally solved. Ended up writing my HTML to a file and then parsing it through a JSON template file. That way, single- and double-quotes didn't give me any problems.

    If anybody has the same problem and needs help, send me a pm!

      If anybody has the same problem and needs help, send me a pm!

      Why don't you just post your solution? That will help people even when you are unreachable.


      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1199261]
Approved by haukex
Front-paged by Corion
[ambrus]: the annotation mentions Brisbane as even worse, and I add Debrecen
[marto]: :)
[ambrus]: robby: dunno, I would like to visit London some time, although there are other places I'd like to visit more
[marto]: "Okay, sure, there are in fact museums and art galleries and other culturally enlightened things to do in Glasgow, but that wouldn't have made a funny comic"
LanX .oO( cemetery breweries ? )

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2017-12-15 11:09 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (431 votes). Check out past polls.