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

A

andipfaff

Hi everybody,

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?

Thanks in advance
Andi
 
A

andipfaff

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.

Ben

Hi,

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.

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.

This is the code:
$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);
$msg->attach(Type => 'text/html; charset=utf-8', Data => $pagetosend,
Encoding => 'Base64');
$msg->attach(Type => 'application/pdf', Path =>
$mail_attachment_filename, Filename => $mail_attachment_filename,
Disposition => 'attachment');
$msg_body = $msg->as_string();
#method 1
MIME::Lite->send('smtp', $email_server, Port=>$email_server_port);
$msg->send();
#method 2
$msg_body = $msg->as_string();
.....
$smtp->datasend($msg_body);
$smtp->dataend();
$smtp->quit;
 
A

andipfaff

Hi,

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.

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.

This is the code:
$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);
$msg->attach(Type => 'text/html; charset=utf-8', Data => $pagetosend,
Encoding => 'Base64');
$msg->attach(Type => 'application/pdf', Path =>
$mail_attachment_filename, Filename => $mail_attachment_filename,
Disposition => 'attachment');
$msg_body = $msg->as_string();
#method 1
MIME::Lite->send('smtp', $email_server, Port=>$email_server_port);
$msg->send();
#method 2
$msg_body = $msg->as_string();
....
$smtp->datasend($msg_body);
$smtp->dataend();
$smtp->quit;- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Sorry I found the error. I did not give the correct mime-type to mime-
lite.
 
A

andipfaff

Quoth andipfaff <[email protected]>:







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


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


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.


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


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.)


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- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

You are absolutely right. THIS code works but I use a variable instead
of pure "application/pdf". This variable was define via my $t_type
inside an if statement. So it was useless outside. After removing "my"
the code was working perfect. You brought me on the right track!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,997
Latest member
mileyka

Latest Threads

Top