develooper Front page | perl.pep | Postings from June 2010

Fwd: Using Email::MIME to convert a plain email to a multipart

Thread Next
Erik Logtenberg
June 6, 2010 13:02
Fwd: Using Email::MIME to convert a plain email to a multipart
Message ID:

I'm running into several unexpected behaviours from Email::MIME after
using parts_set and its wrapper parts_add.

1) Most notably changes to parts have no effect after they've been added
to the email, for instance in the example below, if I were to append the
$part2->header_set("Foo", "Bar");
Now print $part2->as_string();  will print the part including the new
header, but print $email->as_string(); will print the email containing
that part without its new header.

2) When implicitly converting a single part message to multipart by
using parts_add(), the original body will be converted into a part.
However this part will also contain all original headers from the email

Last but not least, I should note that I am merely guessing what the
recommended way is to convert a non-MIME or single part MIME email to
multipart. The only reference I could find in the manual is in the
description of the parts_set method:

"Replaces the parts for an object. Accepts a reference to a list of
Email::MIME objects, representing the new parts. If this message was
originally a single part, the Content-Type header will be changed to
multipart/mixed, and given a new boundary attribute."

Well that seems clear enough. Nevertheless it would help if someone
could indicate wether or not this is indeed the recommended way to
convert a single part mail to multipart. Also I'm also just guessing
that after adding parts one should still be able to change them. This is
not explicitly stated in the documentation (neither is the opposite), so
it would also help if someone could confirm that this is indeed the
expected behaviour.

Thank you in advance and kind regards,


-------- Original Message --------
Subject: Using Email::MIME to convert a plain email to a multipart
Date: Fri, 04 Jun 2010 22:51:20 +0200
From: Erik Logtenberg <>


I'm trying to use Email::MIME to convert a plain email (not MIME) to a
MIME multipart mail. According to the manual I can use the parts_set
method for this purpose because it'll convert a plain email to multipart
if necessary.

So I add the original body as part 1 and a random second part as follows:

$email = Email::MIME->new(<STDIN>);

my $part1 = Email::MIME->create(
    attributes => { content_type => "text/plain" },
    body => $email->body);
my $part2 = Email::MIME->create(
    attributes => { content_type => "text/plain" },
    body => 'part 2');

$email->parts_set([ $part1, $part2 ]);

print $email->as_string();

Now what this does is not exactly as I expected. The result is indeed a
multipart email with both parts. However looking closely I noticed that
each part has both a Date and a MIME-Version header. Those I did not
expect. Moreover I also noticed that the email headers are appended by a
"Content-Type: multipart/mixed" header, but not with a MIME-Version
header. So I have two MIME-Version headers instead of one, but neither
are in the right place.

Now I can understand this to some degree, since I use
Email::MIME->create() to create the parts. The documentation of create()
does specify that a Date-header will be added, since it's mandatory (at
least, within the context of an email itself). So even though methods
like parts() do return the parts as Email::MIME instances, apparently
the create() method is not the right way to create a simple part,
without automatically creating headers that are only relevant for the
email itself.

So... how does one create simple parts? Or if this is not the way at
all: how does one convert a non-MIME email to MIME multipart?

Kind regards,


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About