http://www.perlmonks.org?node_id=525001

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

i'm trying to send 'mixed' email: a message where the MUA decides which type to display (HTML or plaintext)

I've read through the perldoc for MIME::Lite and used the following to contruct the email:

my $mailObj = MIME::Lite->new( From => 'admin@domain.com', To => $email, Subject => "subject line here", Type => 'multipart/alternative' ); $mailObj->attach( Type => 'text/html', Data => $htmlMail, ); $mailObj->attach( Type => 'text/plain', Data => $textMail, ); $mailObj->send();
the problem with the code: the HTML portion isn't coming through. using 'multipart/mixed' does send both portions, renders the HTML mail, but (as expected) attaches the text portion of the email -- which isn't the behavior i want.

i'm sure i'm missing something, but i'm not sure which direction i need to go.

EDIT: the problem is isolated to Thunderbird. Outlook handles the message appropriately, but Thunderbird ignores the HTML content.

EDIT 2: the problem is Thunderbird. it displays the last recognized MIME type, so if the message is built as above, the text-only portion is displayed. reversing the order builds a message so that Thunderbird does, in fact, display the HTML when 'Show Original HTML' is selected as the preference. (per this mozillazine.org forum post)

Replies are listed 'Best First'.
Re: problem w/ MIME::Lite and multipart/alternative emails
by tirwhan (Abbot) on Jan 23, 2006 at 18:47 UTC

    Hmm, works fine for me (what exactly do you mean by "isn't coming through"?):

    use MIME::Lite; $email='me@home.com'; $htmlMail = "<html></html>"; $textMail = "texting"; my $mailObj = MIME::Lite->new( From => 'admin@domain.com', To => $email, Subject => "subject line here", Type => 'multipart/alternative' ); $mailObj->attach( Type => 'text/html', Data => $htmlMail, ); $mailObj->attach( Type => 'text/plain', Data => $textMail, ); print $mailObj->as_string(); __OUTPUT__ Content-Transfer-Encoding: binary Content-Type: multipart/alternative; boundary="_----------=_1138042047 +111520" MIME-Version: 1.0 X-Mailer: MIME::Lite 3.01 (F2.72; T1.15; A1.62; B3.04; Q3.03) Date: Mon, 23 Jan 2006 18:47:27 UT From: admin@domain.com To: me@home.com Subject: subject line here This is a multi-part message in MIME format. --_----------=_1138042047111520 Content-Disposition: inline Content-Length: 13 Content-Transfer-Encoding: binary Content-Type: text/html <html></html> --_----------=_1138042047111520 Content-Disposition: inline Content-Length: 7 Content-Transfer-Encoding: binary Content-Type: text/plain texting --_----------=_1138042047111520--

    There are ten types of people: those that understand binary and those that don't.
      Hmm, works fine for me (what exactly do you mean by "isn't coming through"?):

      I'm not getting the rendered display of the message in my mailreader. I've looked at the source, and it is, in fact, there ... but my mailreader (Thunderbird) isn't rendering it).

        So this is actually a Thunderbird question, not a Perl question, right ;-)? With multipart/alternative you'd expect the client to only show one of the alternative contents (that's what that Content-Type is for). Go to your "View"->"Message body" menu in Thunderbird and fiddle with the settings there, it gives you some options on what you want displayed.


        There are ten types of people: those that understand binary and those that don't.

        I had this exact same problem when trying to create a program which would email Lotus Notes users.

        It has been a while, but if I remember correctly the solution was to send the source in the text portion of the message and include the HTML as a multipart attachment. It was something very odd and hacky, but it solved the problem for Notes. If this is something that you truly need to see in Thunderbird, trying doing a combination of attaching one part and putting the other in the base message.

Re: problem w/ MIME::Lite and multipart/alternative emails
by helphand (Pilgrim) on Jan 24, 2006 at 04:48 UTC

    The rfc's http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html on multipart/alternative specifically deal with the display preferences;

    In general, choosing the best type means displaying only the LAST part that can be displayed....In general, user agents that compose multipart/alternative entities should place the body parts in increasing order of preference, that is, with the preferred format last....From an implementor's perspective, it might seem more sensible to reverse this ordering, and have the plainest alternative last. However, placing the plainest alternative first is the friendliest possible option when mutlipart/alternative entities are viewed using a non-MIME- compliant mail reader.

    Scott