CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
encoding txt file

 



mybend
Novice

Jun 16, 2010, 12:24 PM

Post #1 of 3 (1979 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?




Code
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;



Bianca
User

Jun 16, 2010, 11:45 PM

Post #2 of 3 (1964 views)
Re: [mybend] encoding txt file [In reply to] Can't Post

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.


mybend
Novice

Jun 17, 2010, 4:33 AM

Post #3 of 3 (1959 views)
Re: [Bianca] encoding txt file [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives