in reply to MIME::Lite and Multipart

If you want to have text and html versions of the message then you should combine them in mutipart/alternative. If you want to add an attachment then you should create multipart/mixed which would contain multipart/alternative with text and html, and attachment. So message structure should be as follows:

multipart/mixed - multipart/alternative - text/plain - text/html - application/pdf or whatever you want
Here's the small example that shows how to create such message:
use strict; use warnings; use MIME::Lite; my $alternative = MIME::Lite->new( Type => 'multipart/alternative', ); $alternative->attach( Type => 'TEXT', Data => 'Message text', ); $alternative->attach( Type => 'text/html', Data => '<p><b>Message text</b></p>', ); my $message = MIME::Lite->new( Type => 'multipart/mixed', From => 'sender@example.com', To => 'rcpt@example.com', Subject => 'Example', ); $message->attach($alternative); $message->attach( Type => 'TEXT', Filename => 'passwd', Disposition => 'attachment', Path => '/etc/passwd', ); $message->print(\*STDOUT);

Replies are listed 'Best First'.
Re^2: MIME::Lite and Multipart
by Analog (Novice) on Sep 29, 2009 at 21:20 UTC
    Thanks zwon, I haven't tried it yet but that seems to make a little more sense.

    I still don't understand why the "Type" can't be added by itself. Even in your example I need to know if there is a attachment before I create the "my $message" :Lite object right?

      I still don't understand why the "Type" can't be added by itself.

      Just a note before forwarding you to perldoc, the module source code and zwon for further study:

      you correctly wrote in the opener: $message->replace(Type=>'multipart/mixed');, and this indeed **works** and changes the mail structure you set with the Type=>... assignment during ->new/->build.

      Without an explicite Type, Dumper shows a non-MIME text/plain message. Adding mime parts seems to turn the original body (that you never defined in the opener or Re^4) into our undefined problem mime part, while also turning the Type to multipart/mixed. If my assumption is correct, just setting Data=>"" (and never setting a Type at all) before adding attachments should also avoid the undef bugs.

        you correctly wrote in the opener: $message->replace(Type=>'multipart/mixed');, and this indeed **works**

        No, that's not correct! It creates multipart/mixed with three parts, and it should be multipart/mixed with two parts one of which multipart/alternative and other is attachment.

      Yes, you should know if there's attachment, because if there's no attachment you should create only one MIME::Lite object (multipart/alternative message) and there's no need to create $message at all.

        I understand and thank you guys for the help - I don't know if I agree though, IMO I don't think its right to need to create 2 separate :Lite objects, because if there is no attachment then I need to add the "To", "From", "Subject" keys to that "$alternative" object.

        I'm not going to complain though, MIME:Lite is a excellent module and I'm lucky it exists.

Re^2: MIME::Lite and Multipart
by jakobi (Pilgrim) on Sep 29, 2009 at 19:53 UTC

    Dear Deacon, I think he already had pretty much the same skeleton down pat and thus deserves help beyond pure rtfm and rtfm+keyword levels.

    He managed to stumble twice upon bugs that could be triggered by undef (both of which he might have avoided by exactly sticking to the skeleton), but I'd argue that triggering those bugs actually demonstrated some understanding and skill. Which is what we want to cultivate beyond copy/pasting boilerplate code.

      he already had pretty much the same skeleton down pat and thus deserves help beyond rtfm keyword level.

      Take another look onto my code and compare it with Analog's code. It's not about MIME::Lite usage and bugs in his code, it's about structure of resulting message.

        Point noted. The structure and function ordering in the skeleton _is_ way cleaner :). But I wouldn't hold debug comments and remnants against Analog (for the next 2 days at least :) ).