http://www.perlmonks.org?node_id=1199261

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 <john@gmail.com>" }, { "To": "Jane Doe <jane@gmail.com>" } ], "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

    xMonk:

    What sort of difficulties are you having?

    ...roboticus

    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 <john@doe.com.au>" }, { "To": "Jane Doe <jane@doe.com.au>" } ], "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.

      Alexander

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