Reputation: 5197
I'm trying to figure out why my multi-part email all comes out as one:
The code I'm using to create 2 "parts" for my email, is:
use Email::MIME;
use Email::Address::XS;
use Email::Sender::Simple qw(sendmail);
# multipart message
my @parts = (
Email::MIME->create(
attributes => {
content_type => "text/plain",
disposition => "inline",
charset => "UTF-8",
},
body_str => "Hello there é!",
attributes => {
encoding => 'quoted-printable',
charset => "UTF-8",
}
),
Email::MIME->create(
attributes => {
content_type => "text/html",
disposition => "inline",
charset => "UTF-8",
},
body_str => "Hello there éíó!",
attributes => {
encoding => 'quoted-printable',
charset => "UTF-8",
}
)
);
my $email = Email::MIME->create(
header_str => [
From => '[email protected]',
To => [ 'Name <[email protected]>' ],
Subject => "foo"
]
);
$email->parts_set( \@parts );
print $email->as_string;
sendmail($email->as_string);
The email is sent and comes out like so:
Delivered-To: [email protected]
Received: by 10.223.130.5 with SMTP id 5csp329339wrb;
Thu, 11 Jan 2018 01:09:17 -0800 (PST)
X-Google-Smtp-Source: ACJfBotkcH6W0rsnQMkLx9pPqbt1rUgKaC9ShvWLQbn6u8muQbVnCjTCZRmf0d5RzegqW4AGpFHP
X-Received: by 10.28.92.146 with SMTP id q140mr474098wmb.5.1515661757887;
Thu, 11 Jan 2018 01:09:17 -0800 (PST)
Return-Path: <[email protected]>
From: [email protected]
To: Name <[email protected]>
Subject: foo
Date: Thu, 11 Jan 2018 09:09:17 +0000
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Type: multipart/mixed; boundary="15156617570.82fd1E.6414"
Message-Id: <[email protected]>
--15156617570.82fd1E.6414
Date: Thu, 11 Jan 2018 09:09:17 +0000
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Hello there =C3=A9!
--15156617570.82fd1E.6414
Date: Thu, 11 Jan 2018 09:09:17 +0000
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Hello there =C3=A9=C3=AD=C3=B3!
--15156617570.82fd1E.6414--
I can see the split "parts", but for some reason they show up together?
UPDATE: : I thought I had fixed this - I had 2 of these attributes => { }
values for each @part
, and that was causing the content_type to be text/plain as a default. So I now have:
my @parts = (
Email::MIME->create(
body_str => "Hello there <b>é!</b>",
attributes => {
encoding => 'quoted-printable',
charset => "UTF-8",
content_type => "text/plain",
disposition => "inline",
}
),
Email::MIME->create(
body_str => "Hello there <b>éíó!</b>",
attributes => {
content_type => "text/html",
disposition => "inline",
charset => "UTF-8",
encoding => 'quoted-printable',
}
)
);
...and the email comes out as:
From: [email protected]
To: Name <[email protected]>
Subject: foo
Date: Thu, 11 Jan 2018 09:45:21 +0000
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Type: multipart/mixed; boundary="15156639210.d5dF6bf.14583"
--15156639210.d5dF6bf.14583
Date: Thu, 11 Jan 2018 09:45:21 +0000
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Hello there <b>=C3=A9!</b>
--15156639210.d5dF6bf.14583
Date: Thu, 11 Jan 2018 09:45:21 +0000
MIME-Version: 1.0
Content-Type: text/html; charset="UTF-8"
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Hello there <b>=C3=A9=C3=AD=C3=B3!</b>
--15156639210.d5dF6bf.14583--
...but its still showing both the plain text and HTML parts at the same time. The weird part, is that if I put tags around some of it, I get what I would expect:
...but it just shows them both side by side :/
Upvotes: 1
Views: 346
Reputation: 5197
I finally worked it out by comparing an email that I knew worked as I was expecting, to mine.
The emails I was sending out, had:
Content-Type: multipart/mixed; boundary="_----------=xxxxx"
But the working ones had:
Content-Type: multipart/alternative; boundary="_----------=xxxxx"
Sure enough, seeing the multipart/mixed, it now works fine:
$email->content_type_set( 'multipart/alternative' );
Upvotes: 1
Reputation: 69274
I think you misunderstand what a multi-part email is. A multi-part email is a single email which is made up of many parts. Each part can be another email message or the contents of another file.
You have created a single email which contains a number of other emails.
If you want to send separate emails then you need to... well... send separate emails :-)
Update: Ok, so it was the terminology that was wrong here. The OP didn't actually want a multi-part email to be separate emails, he wanted the separate parts to be displayed separately in his email client. After a minor tweak to his code, the email created looks correct - but Gmail is (for some reason) displaying the two parts (one plain text and the other HTML) at the same time.
Upvotes: 2
Reputation: 1056
I'm not sure what's the question, but I could try to open up what's going on in there. So the parts you have defined are meant for different content-types and transfer encodings for example attachment's etc. So when you use boundary it can differentiate between these parts that have different content and encodings so it can be interpreted correctly when received. I assume you thought it would split the message to multiple parts and send them separately?
Upvotes: 1