Skip to Content

Perl MIME::Lite - Multipart/Alternative

I was using MIME::Lite in a Perl script recently, but ran across an issue. The issue is that I wanted to create a message that was multipart/alternative, so it would have a text friendly portion, an HTML portion, and (the part that caused my frustration) the HTML portion needed to be able to contain pictures within the HTML, not just added as attachments.

The problem was that every time I would attempt to craft such a message, the pictures I would attach wouldn't show up in the HTML. They would come across as attachments or not show up at all. The issue is because for the HTML to contain images, the HTML piece needs to be set as a type 'Multipart/Related', while the main message needs to be set as 'Multipart/Alternative'. Perhaps I just overlooked the obvious, but I never saw an example of how to do this. It gets pretty easy when you think of it logically though.

First, just create the main message like so ...

  1. my $msg = MIME::Lite::->new(
  2. 'To' => 'somedude@somedomain.com',
  3. 'From' => 'someotherdude@someotherdomain.com',
  4. 'Subject' => 'Hello',
  5. 'Type' => 'multipart/alternative',
  6. );

Ok, now create the text part separately....

  1. my $text_part = MIME::Lite::->new(
  2. 'Type' => "text/plain",
  3. 'Data' => 'just a message',
  4. );

Ok, now another part for the HTML, setting the type on this one to 'Multipart/Related'.

  1. my $html_part = MIME::Lite::->new(
  2. 'Type' => 'multipart/related',
  3. );

Add the HTML you want. Include the picture. Then attach the picture to the HTML part.

  1. $html_part->attach(
  2. 'Type' => 'text/html',
  3. 'Data' => "<b>look at this picture!</b><br><img src=\"cid:picture.png\">",
  4. );
  5.  
  6. $html_part->attach(
  7. 'Type' => "image/png",
  8. 'Id' => "picture.png",
  9. 'Path' => "/path/to/picture.png",
  10. );

Lastly, attach the text part to the main message, then attach the HTML part. This is actually the order in which mail clients prefer it.

  1. $msg->attach($text_part);
  2. $msg->attach($html_part);

There, now we have a message with text, html, and pictures in the html. You can then just send it off using MIME::Lite or you can output the message back as a string and send it via Net::SMTP , or whatever you prefer. I prefer Net::SMTP, so to get it as a string to send via that, just do ...

  1. my $email = $msg->as_string();

Hopefully this information may save someone lots of frustration =D

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thank you very much!!!

Thank you very much!!!

Thanks for posting this! An

Thanks for posting this! An excellent example and one that I also found to be missing from CPAN and such.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA
Are you are a human visitor?