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

Discussion in 'Perl Misc' started by andipfaff, Oct 23, 2011.

  1. andipfaff

    andipfaff Guest

    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
     
    andipfaff, Oct 23, 2011
    #1
    1. Advertising

  2. andipfaff

    andipfaff Guest

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

    On 23 Okt., 19:06, Ben Morrow <> wrote:
    > Quoth 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?

    >
    > 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;
     
    andipfaff, Oct 23, 2011
    #2
    1. Advertising

  3. andipfaff

    andipfaff Guest

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

    On 23 Okt., 19:28, andipfaff <> wrote:
    > On 23 Okt., 19:06, Ben Morrow <> wrote:
    >
    >
    >
    >
    >
    > > Quoth 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?

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


    Sorry I found the error. I did not give the correct mime-type to mime-
    lite.
     
    andipfaff, Oct 23, 2011
    #3
  4. andipfaff

    andipfaff Guest

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

    On 23 Okt., 21:00, Ben Morrow <> wrote:
    > Quoth andipfaff <>:
    >
    >
    >
    >
    >
    > > On 23 Okt., 19:06, Ben Morrow <> wrote:
    > > > Quoth andipfaff <>:

    >
    > > > > 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- 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!
     
    andipfaff, Oct 24, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    413
    Fredrik Lundh
    Aug 13, 2008
  2. codewarrior
    Replies:
    0
    Views:
    535
    codewarrior
    May 4, 2009
  3. Peter Green
    Replies:
    0
    Views:
    645
    Peter Green
    Nov 5, 2009
  4. Gunnar Hjalmarsson

    Net::SMTP and attachments

    Gunnar Hjalmarsson, Aug 23, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    138
    Tony Curtis
    Aug 23, 2004
  5. Jim Roberts
    Replies:
    7
    Views:
    244
    Bart Lateur
    Jan 4, 2006
Loading...

Share This Page