Re: Net-SMTP on Win32 converts \n to \r\n and destroys PDF attachments etc.




Quoth andipfaff <andreas.pfaff.wangen@xxxxxxxxx>:
On 23 Okt., 19:06, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
Quoth andipfaff <andreas.pfaff.wan...@xxxxxxxxx>:

I try to send emails with an attched PDF on Windows with ActiveState
v5.8.9. I am using Mime-Lite to create the message an as_string() to
convert it to a variable.
$msg_body = $msg->as_string();

If I write this variable to a file with binmode the \n in the PDF is
written as \n.
When sending the Mail the attached PDF is unreadable because the \n
have been replaced by \r\n somewhere. I suggest this happend in
$smtp->datasend($msg_body);

How can I tell Perl or the library to handle this in binmode?

You can't. Bare \ns are illegal in a mail message: you need to base64
encode files like PDFs which contain binary data.

How are you attaching the PDF? If you tell MIME::Lite it's a PDF, with a
Type => "application/pdf" parameter, it should handle the base64 for you
automatically.

I am using mime-lite to attach it. Type is application/pdf but I
cannot add the Encoding: Base64 because the script will no longer send
the mail then (without error message). I have to leave the encoding
away and then it is being sent 8Bit encoded.

That's not what I see. If I run the code below I get a mail message with
two base64-encoded parts.

But I do not think mime-lite ist the problem. Just sending nothing
else then one \n in a mailbody will result in a 0x0D0x0A in the
received mail. Even using mime-lite for sending the mail instead of
net-smtp and datasend() will do that.

Yes. *This is correct*. Read RFCs (err...) 5321 and 5322.

This is the code:

Some basic remarks, unrelated to MIME::Lite:

You don't appear to be using 'strict'. You should.

If you format your code a bit better you and everyone else will find
it much easier to understand.

The code you've posted can't be run directly: it needs a whole lot
of variables set which you don't provide values for. This make it
much harder for people to help you, and therefore much less likely
they'll bother.

$pagetosend = qq(<html>\r\n<body>$mail_text\r\n</body>\r\n</html>);
$msg = MIME::Lite->new (From => $email_abs, To => $email_empf, Subject
=> $db_mail_subject, Type => 'multipart/Mixed', Data => $pagetosend);

You don't need Data with multipart types: you use ->attach instead.

$msg->attach(Type => 'text/html; charset=utf-8', Data => $pagetosend,
Encoding => 'Base64');

HTML doesn't usually need to be Base64. Even if you don't think you can
get away with 8-bit, quoted-printable is enough for UTF-8. (You need
base64 for encodings like UTF-16, but using them in email is just
silly.)

$msg->attach(Type => 'application/pdf', Path =>
$mail_attachment_filename, Filename => $mail_attachment_filename,
Disposition => 'attachment');
$msg_body = $msg->as_string();

If I print the message at this point I see

MIME-Version: 1.0
Content-Transfer-Encoding: binary
Content-Type: multipart/mixed;
boundary="_----------=_1319396295126140"
X-Mailer: MIME::Lite 3.027 (F2.78; T1.31; A2.07; B3.13; Q3.13)
Date: Sun, 23 Oct 2011 19:58:15 +0100

This is a multi-part message in MIME format.

--_----------=_1319396295126140
Content-Disposition: inline
Content-Transfer-Encoding: base64
Content-Type: text/html; charset=utf-8

PGh0bWw+DQo8Ym9keT4NCjwvYm9keT4NCjwvaHRtbD4=

--_----------=_1319396295126140
Content-Disposition: attachment; filename="fakepdf"
Content-Transfer-Encoding: base64
Content-Type: application/pdf; name="fakepdf"

JVBERi0xLjQKcGVlZHkgcGVlZHkgcGVlZHllZmYK

--_----------=_1319396295126140--

with the 'PDF' base64 encoded. Do you not?

Ben

.



Relevant Pages