in reply to Multi-Part email with Attachments using MIME::Lite

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

Replies are listed 'Best First'.
Re: Re: Multi-Part email with Attachments using MIME::Lite
by filmo (Scribe) on Aug 16, 2001 at 04:09 UTC
    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

        Thanks for the help. After some playing around the MIME::Lite, I think I've figured it out. (at least it seems that way because the e-mails are working correctly now.) The trick was to build two MIMEs and attach the second one to the first

        Here's the modified code

        $email_msg = MIME::Lite->new ( Return-Path =>$from, From =>$from, To =>$email_to, Subject =>"Completed Transcripts ($count_trans +attached)", Type =>'multipart/mixed', Encoding =>'7bit' ); if ($cc_email) {$email_msg->add(CC =>$cc_email)} # Build Part 2 which is 'multipart/alternative' $body = MIME::Lite->new ( Type =>'multipart/alternative', Encoding =>'7bit', ); # Here's the fallback plain text message $body->attach( Type =>'text/plain', Encoding =>'7bit', Data =>$plain ); # Here's the HTML, nicely formated e-mail message + $body->attach( Type =>'text/html', Encoding =>'7bit', Data =>$html_body, ); # Attach the "body" part to the original message $email_msg->attach($body); # Attach "attachments" to original message foreach $file (sort keys %file_attach) { $email_msg->attach (Type =>'application/msword', Path =>$file_attach{$file}, Filename=>$file, Disposition=>'attachment', Encoding =>'base64' ); }
        This builds a "correctly" nested e-mail.
        --
        Filmo the Klown