
mybend
Novice
Jun 16, 2010, 12:24 PM
Post #1 of 3
(3816 views)
|
encoding txt file
|
Can't Post
|
|
Hi, 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 "test #’I:ógõ“£ÅIK" 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. --$boundary Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Attached is a test file. --$boundary Content-Type: $ct; name=$attachment Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=$attachment $encode --$boundary-- EOD my $smtp = Net::SMTP->new($smtp_server, Timeout => 60) or die "Cannot connect to smtp server"; $smtp->auth($user,$pass); $smtp->mail($ENV{USER}); $smtp->to($to); $smtp->data(); $smtp->datasend($msg); $smtp->dataend(); $smtp->quit;
|