I'm trying to using MIME::Lite to generate HTML e-mail with multiple attachments.

I want to be able to send MS Word documents with a simple message in both plain text and HTML to various clients.

The generation of the plain text and html portion work fine and are viewable by various e-mail clients. The problems is the attachements which don't seem to be attaching correctly. (see my suspicions below regarding nesting of "multipart/alternative" within "multipart/mixed").

Here's the code I've got so far.

$file_attach{'testdoc.doc'} = '/home/path/to/document/testdoc.doc'; etc... # I've tested the %file_attach hash and it does contain the correct pa +th and file name. $email_msg = MIME::Lite->new ( Return-Path=>'me@here.com', From =>'me@here.com', To =>$email_to, Subject =>"Completed Documents ($doc_count +attached)", Type =>'multipart/alternative', ); $email_msg->attach (Type =>'text/plain', Encoding=>'7bit', Data =>$plain ); $email_msg->attach (Type =>'text/html', Data =>$html_body, Encoding =>'7bit', ); foreach $file (keys %file_attach) { $email_msg->attach (Type =>'application/msword', Path =>$file_attach{$file}, Filename=>$file, Disposition=>'attachment', Encoding =>'base64' };
The problems seems to be that e-mail created isn't "nested correctly". The current code creates a message that is:
(header) Content-Type: multipart/alternative; boundary="_----------=_AAA" (body) This is a multi-part message in MIME format. --_----------=_AAA Content-Type: text/plain Plain Text Message Here --_----------=_AAA Content-Type: text/html <B>HTML Message Here<B><BR> --_----------=_AAA Content-Disposition: attachment; filename="test2.doc" Content-Transfer-Encoding: base64 Content-Type: application/msword; name="test2.doc" --_----------=_AAA--
(I've changed the boundary to "AAA" for illustrative purposes herein) Given that e-mail messages I've sent myself with attachements appear as below, I think the code needs to be restructured to create the nesting of the "multipart/alternative" within the "multipart/mixed" as illustrated below.
(header) Content-Type: multipart/mixed; boundary="----=_NextPart_000_AAAAA" (body) This is a multi-part message in MIME format. ------=_NextPart_000_AAAAA Content-Type: multipart/alternative; boundary="----=_NextPart_001_BBBBB" ------=_NextPart_001_BBBBB Content-Type: text/plain; Plain Text Message Here ------=_NextPart_001_BBBBB Content-Type: text/html; <B>HTML Message Here</B><br> ------=_NextPart_001_BBBBB-- ------=_NextPart_000_AAAAA Content-Type: application/msword; name="test.doc" Content-Transfer-Encoding: base64
(The distinction between the two is that the last example "nests" the plain/html email message within a "multipart/mixed" as opposed to the first which calls it a "multipart/alternative" and places the attachments within the same context as the plain/HTML e-mail message.)

So I guess the real question is how do I use MIME::Lite to create the working "nested" version.
--
Filmo the Klown


In reply to Multi-Part email with Attachments using MIME::Lite by filmo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.