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

I hope someone can help me. I've tried everything I can think of. I am using mime::lite to try to send an email which will have as the "body" of the mail -- the data from a .txt file. It will also have an attachment of another .txt file. I can get it to attach either one, or both files, but cannot get it to have the data I want in the body with the attachment as well. I see no way of doing this. If you code more than one "attach" option, it attaches both files. If I just have one "attach" block of code, with no disposition, it gives me the body the way I want it, but as soon as I try to attach the other file with disposition of "attachment", then both files show as attachments, with nothing in the body of the message. I'm thinking this can't be done using mime::lite. PLEASE HELP!

Replies are listed 'Best First'.
Re: mime::lite problem
by dorko (Prior) on Nov 23, 2005 at 21:28 UTC
    Working with what derby wrote, would explictly setting the disposition of the "body attachment" to "inline" help?
    # body $msg->attach( Type => 'TEXT', Data => $data, Disposition => 'inline', # <== extra line here ); # attachment $msg->attach( Type => 'text/plain', Path => $ARGV[0], Filename => $ARGV[0], Disposition => 'attachment', );
    The docs do say that "inline" is the default, so this might not help much.

    Furthermore, the docs for MIME::Lite say:

    Content-disposition

    You can safely scrub the "content-disposition" attribute if you trust the mail reader to do the right thing when it decides whether to show an attachment inline or as a link. Be aware that scrubbing both the content-disposition and the content-type means that there is no way to "recommend" a filename for the attachment!

    Note: there are reports of brain-dead MUAs out there that do the wrong thing if you provide the content-disposition. If your attachments keep showing up inline or vice-versa, try scrubbing this attribute.

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Re: mime::lite problem
by derby (Abbot) on Nov 23, 2005 at 20:00 UTC

    Where's the code? Oh yeah, it's in the docs.

    Okay not fully but enough to get your started. Here's the doc code a bit expanded:

    #!/usr/bin/perl -wd use MIME::Lite; use strict; die "usage: $0 file" unless @ARGV == 1; open my $fh, "<$ARGV[0]" or die "Can't open $ARGV[0]: $!"; local( $/ ) = undef; my $data = <$fh>; close $fh; my $msg = MIME::Lite->new( From =>'me@myhost.com', To =>'you@yourhost.com', Subject =>'A message with 2 parts...', Type =>'multipart/mixed' ); $msg->attach( Type => 'TEXT', Data => $data, ); $msg->attach( Type => 'text/plain', Path => $ARGV[0], Filename => $ARGV[0], Disposition => 'attachment' ); $msg->send;

    If this does not show up as text body and an attachment, then double check your email client - it may not handle MIME well.

    -derby

      If this does not show up as text body and an attachment, then double check your email client - it may not handle MIME well.

      try print $msg->as_string; instead of using the mail client. Your approach als shows up with a "Content-Type: multipart/mixed", and I believe that's what the OP didn't want.

      The answer is, MIME::Lite is as documented, well suited to do either the "multipart/mixed" or a single content mail message. For other usages, use a different module.

      Well, there is more documentation in MIME::Lite about accessor methods, so you can virtually put anything into your mail message. But why use MIME::Lite for that purpose?

      Cheers, Sören

      I'm not exactly sure which parts to substitute in your code. Here is my code, which has partially worked, as I've noted, but not as I wish. Earlier in the script, I am opening filehandles, and writing them to .txt files to create the attachments (i.e.):
      open (ORDER_DATA, ">$order_dir/temp_order.txt") || die "Can't open $or +der_dir/temp_order.txt!\n"; select (ORDER_DATA); &print_form; select (STDOUT); close (ORDER_DATA); Then, here is the mime::lite section: ### Create the multipart "container": $msg = MIME::Lite->new( From =>$FORM{"email"}, To =>$recipient, Subject =>"STI Document Order $FORM{ORDER_NUMBER} +", Type =>'multipart/mixed'); $msg->attach(Path =>"$order_dir/temp_order.txt"); ### Add the attachment: $msg->attach(Path =>"$order_dir/temp_order_attach.txt", Disposition =>'attachment'); $text = $msg->as_string; $msg->send($mailprog);
      Can you tell me the best way to adapt the (ARGV[0], and $fh parts of your code to mine, so I can make this work using my .txt files?
      derby -- Thank you sooooo much. I got this to work using parts of your code.....I guess it was the filehandle syntax that was messing me up. Thanks again so much derby!