in reply to Re^2: MIME voodoo.
in thread MIME voodoo.
Your input isn't correct MIME message. There should be header that defines boundary and it's missed. Here's some small and dirty (sorry...) example:
use strict; use warnings; use 5.010; use MIME::Lite; use Email::MIME; my $msg = MIME::Lite->new( From => 'src@example.com', To => 'dst@example.com', Subject => 'message', Type => 'multipart/alternative', ); $msg->attach( Type => 'text/plain', Data => 'this is text content', ); $msg->attach( Type => 'text/html', Data => 'this is <b>html</b> content', ); my $msg_str = $msg->as_string; print $msg_str; # note the output here -- that's a complete message my $parsed = Email::MIME->new($msg_str); say "*" x 50; if ($parsed->content_type =~ m{^multipart/alternative}) { say get_text_parts($parsed)->body; } sub get_text_parts { my @parts = shift->parts; my %ct; $ct{$_->content_type} = $_ for @parts; return $ct{'text/plain'} if exists $ct{'text/plain'}; return $ct{'text/html'} if exists $ct{'text/html'}; return $parts[0]; }
Upd: minor fix
Upd: Note also that $_->content_type may return something like text/plain; charset=utf-8, and this code will fail in this case.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: MIME voodoo.
by vxp (Pilgrim) on Jul 16, 2009 at 19:37 UTC | |
by zwon (Abbot) on Jul 16, 2009 at 20:00 UTC | |
by vxp (Pilgrim) on Jul 16, 2009 at 20:10 UTC | |
by ikegami (Patriarch) on Jul 16, 2009 at 20:39 UTC | |
by vxp (Pilgrim) on Jul 16, 2009 at 20:54 UTC | |
by zwon (Abbot) on Jul 16, 2009 at 20:40 UTC |