Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Multi-Part email with Attachments using MIME::Lite

by filmo (Scribe)
on Aug 16, 2001 at 01:53 UTC ( [id://105200]=perlquestion: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re: Multi-Part email with Attachments using MIME::Lite
by tachyon (Chancellor) on Aug 16, 2001 at 03:14 UTC

    These examples using MIME::Entity work fine. In the first sub we have plain text, a gif and HTML. In the second we have plain text and MSWord docs. I find MIME::Entity (part of MIME::Tools reliable)

    sub Respond_HTML { use MIME::Entity; # first, format the email address for MIME::ENTITY # create the top-level, and set up the mail headers: my $top = build MIME::Entity Type => 'multipart/mixed', From => $our_email, To => $email, Subject => "Information from $us"; # next, add the body of the message: attach $top Data=> "Hello $name,\n\nHere is the information you requested.\ +n\n$us \n\n"; # now add the pretty leterhead: attach $top Path => "$path_to_home_dir/Images/letterhead.gif", Type => 'image/gif', Encoding => 'base64'; # now, make attachment/s: for ($i=0 ; $i<=4; $i++ ) { if ($request[$i]) { attach $top Path => "$path_to_home_dir/Auto_Respond_Files/$HTML_file +[$i]", Type => 'text/HTML', Encoding => '7bit'; } } # finally, send it: open MAIL, "|$mail_prog -t -i" or die "open: $!"; $top->print(\*MAIL); close MAIL; return 1; } # end Respond_HTML # Send automatic msword.doc email response according to request(s) sub Respond_MSWord { use MIME::Entity; # first, format the email address for MIME::ENTITY # create the top-level, and set up the mail headers: my $top = build MIME::Entity Type => 'multipart/mixed', From => $our_email, To => $email, Subject => "Information from $us"; # next, add the body of the message: attach $top Data=> "Hello $name,\n\nHere is the information you requested.\ +n\n$us\n\n"; # now, make attachment/s: for ($i=0 ; $i<=4; $i++ ) { if ($request[$i]) { attach $top Path => "$path_to_home_dir/Auto_Respond_Files/$msword_fi +le[$i]", Type => 'application/octet-stream', Encoding => 'base64'; } } # finally, send it: open MAIL, "|$mail_prog -t -i" or die "open: $!"; $top->print(\*MAIL); close MAIL; return 1; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Perhaps I missread your reply, but it seems like the two examples simply create a single 'multipart/mixed' MIME. The first one sends some plain text and then attaches an HTML file. The second on sends some plain text and then attaches a word file (even though Type=> is set to octet-stream and not 'msword'.

      What I'm looking to do is created a formatted e-mail body complete with all html code with a fall back to a simple plain text message if the receipient doesn't have a current email browser that can view HTML e-mails. In addition to those two parts, I also want to attach multiple attachements.

      It seems that the correct way to do this is to set the header to 'multipart/mixed' and then define one of the multiparts as 'multipart/alternative' instead of 'text/plain' or 'text/html'. Within that sub-part, the 'text/plain' and 'text/html' are then created. Once the sub-part is "closed-out", then the attachments are added to the original 'multipart/mixed'
      --
      Filmo the Klown

        Now it becomes more clear what you want. I suggest you check out the MIME::Entity docs as you can mainpulate the parts to you hearts content.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-24 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found