Home: Perl Programming Help: Advanced:
encoding txt file


Jun 16, 2010, 12:24 PM

Views: 4239
encoding txt file


I am trying to encode .txt file to send it as an email attachment.

The code works for all kind of files such as pdf, doc, jpg, but it does not proper encode txt file.

I have tried to replace 'Multipart/Mixed' with 'text/x-setext'. That doesn't help.

As the result I get
where the second line is not supposed to be there.

Any ideas?

use strict; 
use warnings;
use net::smtp;
use MIME::Base64;

my $smtp_server = 'mail.somewhere.net goes here';
my $user = 'mail user id goes here';
my $pass = 'mail password goes here';
my $to = 'someone@somewhere.com';
my $from = 'somebody@here.com';
my $attachment = "ncoacodes.pdf";
my $ct = 'Multipart/Mixed';
my $subject = 'automated email test of attached pdf';

my $content;
local $/ = undef;
open IN, $attachment or die "Error opening $attachment: $!";
binmode IN;
$content = <IN>;
close IN;

my $encode = encode_base64($content);

my $boundary = '<--------- BFC:';
my @chrs = ('0'..'9', 'A'..'Z','a'..'z');
foreach (0..16)
$boundary .= $chrs[rand(scalar @chrs)];
$boundary .= "------------->";

my $msg = <<EOD;
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="$boundary"
From: $from
Reply-To: $to
X-Mailer: fubar.pl
To: $to
Date: Wed, 01 Jun 2009 13:12:05 -0500
Subject: $subject

This is a multi-part message in MIME format.

Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Attached is a test file.

Content-Type: $ct; name=$attachment
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=$attachment


my $smtp = Net::SMTP->new($smtp_server, Timeout => 60) or die "Cannot connect to smtp server";


Jun 16, 2010, 11:45 PM

Views: 4224
Re: [mybend] encoding txt file

For multipart Mails I use http://search.cpan.org/~rjbs/MIME-Lite-3.027/lib/MIME/Lite.pm#Create_a_multipart_message
with the as_string method.
And the string will be sended by Net::SMTP because MIME::Lite has no correct error handling in smtp communication.


Jun 17, 2010, 4:33 AM

Views: 4219
Re: [Bianca] encoding txt file

I don't have an option of using MIME::Lite in this case unfortunately... :(