Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Maybe this question doesn't belong here, but I try anyway.
I have written a perl-script which creates a text-file from information in a datebase.
One line can become very long when there are a lot of related records in one table.
When created the file looks OK, but when I send the file as attachement to somebody and they look at the file it is cut-off.
It is broken up in chuncks of 2042 characters, which are broken up in 3 lines: 2 lines of 999 characters ending with a CR/LF and 1 line of 44 characters ending with "!" and CR/LF.
I think it has to do with the MIME-TYPE which I use to send the file.
Here is the code-part:
sub emailFile { my $to = shift; my $subject = shift; my $content = shift; my $filename = shift; my $nette_naam = shift; my $command = "|/usr/lib/sendmail -f $from $to"; open(MAIL, $command) || die("Can't open sendmail!!\n"); print MAIL "To: $to\r\n"; print MAIL "Subject: $subject\r\n"; print MAIL "MIME-Version: 1.0\r\n"; print MAIL "Content-Type: multipart/mixed; boundary=\"----=_NextPa +rt_000_002C_01C2C170.A5EE7000\"\r\n"; print MAIL "\r\n"; print MAIL "This is a multi-part message in MIME format.\r\n"; print MAIL "\r\n"; print MAIL "------=_NextPart_000_002C_01C2C170.A5EE7000\r\n"; print MAIL "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; print MAIL "Content-Transfer-Encoding: 8bit\r\n"; print MAIL "\r\n"; print MAIL $content . today() . "\r\n"; print MAIL "\r\n"; if (open(FILE, $filename)) { print MAIL "------=_NextPart_000_002C_01C2C170.A5EE7000\r\n"; print MAIL "Content-Type: application/octet-stream; name=\"$ne +tte_naam\"\r\n"; print MAIL "Content-Transfer-Encoding: 8bit\r\n"; print MAIL "Content-Disposition: attachment; filename=\"$nette +_naam\"\r\n"; print MAIL "\r\n"; while (<FILE>) { print MAIL $_; } close (FILE); } print MAIL "------=_NextPart_000_002C_01C2C170.A5EE7000--\r\n"; print MAIL "\r\n"; close(MAIL); }
Any help is welcome, and if my post here is wrong, pls tell me were I should post.

Sunclair

Replies are listed 'Best First'.
Re: Mail distorted
by rdfield (Priest) on Nov 12, 2003 at 09:38 UTC
Re: Mail distorted
by cleverett (Friar) on Nov 12, 2003 at 10:32 UTC
    I second the previous motion to use MIME::Lite. Just to amplify on that a bit:

    I know the attachment will work fine if you base64 encode it, binary attachments want base64 encoding.

    MIME::Lite will do all the nitty-gritty work to generate emails. With binary type attachments, it can open the file, base64 encode it, and insert it into the SMTP data as a MIME attachment in a single statement.

Re: Mail distorted
by Anonymous Monk on Nov 12, 2003 at 11:53 UTC

    Besides Mime::Lite, you may also try Mail::Box, which is much more work to install (it's certainly not "Lite"), however more powerful. The following (untested) code is about all you need to do.

      use Mail::Message;
      my $msg = Mail::Message->build
       ( To   => 'to.you@example.com'
       , From => 'me@example.com'
       , file => 'attach.this.file.doc'
       );
      $msg->send(via => sendmail);
    
    See the index of MailBox for details. You need buildFromBody() when you want to set a preamble in the multipart.

Re: Mail distorted
by iburrell (Chaplain) on Nov 13, 2003 at 19:58 UTC
    The problem you are having is because the lines are too long. Mail messages are limited to lines of 998 characters (1000 characters with the CRLF).

    The Content-Type is irrelevant to the encoding. The type is for the client to determine how to present the attachment. It sounded you are sending a text file; text/plain is likely the right type to use.

    If you can't shorten the lines in the file, then you need to use an Content-Transfer-Encoding. If you want to preserve the file, then base64 is the right encoding to use. If you just care about protecting high-bit characters and line lengths but don't care about control characters, then quoted-printable may work. The advantage of quoted-printable is that the attachment is still readable, just with non-ASCII values and line endings encoded with the equals signs you sometimes see.

Re: Mail distorted
by Paulster2 (Priest) on Nov 12, 2003 at 19:02 UTC

    Just to throw in my two cents worth answering the unanswered: It would appear that this is exactly the place to put code questions. In the future though you might want to check out the Need Help? link at the upper right hand side of the window (just below the Perl Monks rotating image and above the Chatterbox) to answer those questions. Don't worry, I'm a newby, and everyone who uses this site was a newby once, also!!