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: Beginner:
Issue with attaching 2 files in email using Perl Sendmail

 



zaxxon25
New User

Jul 20, 2008, 12:18 PM

Post #1 of 2 (321 views)
Issue with attaching 2 files in email using Perl Sendmail Can't Post

I am having an issue with attaching 2 attachments one xip and other excel file with email and sending using perl sendmail. I tried to look on various forums and everywhere i get advice using MIME :: Lite. Unfortunately i cannot use it . I have tried to write a program on my own to send 2 attachments and sending using perl sendmail but program is not working. It is reading only first file for attachment purposes and ignores second one.I would appreciate if anyone can pinpoint error with my program.

use strict;
use warnings;
use Mail::Sendmail;
use MIME::QuotedPrint;
use MIME::Base64;

my $file1 = 'c:\test1.xls';
my $file2 = 'c:\sample.zip';


my $file3 = $file2 || $file1; # file to attach

my %mail = ( To => "abc@yahoo.com",
From => "def@yahoo.com",
Cc => "hij@yahoo.com",
Subject => "Two Attachments",
smtp => 'mac.abc.com'
);

my $content;
{ local $/ = undef; # slurp file
open IN, $file3 or die "Error opening $file3: $!";
binmode IN; $content = <IN>;
$mail{body} += encode_base64(<IN>);
close IN;
}


my $boundary = "====" . time() . "====";
$mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";

my $message = encode_qp( $email_body );

my $len = length $$mail{body};
$mail{body} = <<EOD;


$boundary
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

$message
$boundary

Content-Type: application/octet-stream; name="$file3"
Content-Disposition: attachment; filename="$file3"
Content-Transfer-Encoding: base64
Content-Length: $len

$mail{body}
--$boundary
Content-Type: application/vnd.ms-excel; name="$file3"
Content-Disposition: attachment; filename="$file3"
Content-Transfer-Encoding: base64
Content-Length: $len

$mail{body}
$boundary--
END_OF_BODY



sendmail(%mail) or die $Mail::Sendmail::error;

open(OUT, ">>$sendmail_log") or die "Cannot open LOG file $file3: $!";
print OUT $Mail::Sendmail::log;
print OUT "\n==============================================================================\n";
close(OUT);


KevinR
Veteran


Jul 20, 2008, 12:58 PM

Post #2 of 2 (317 views)
Re: [zaxxon25] Issue with attaching 2 files in email using Perl Sendmail [In reply to] Can't Post

well, at minimum these need to be fixed:

my %mail = ( To => "abc@yahoo.com",
From => "def@yahoo.com",
Cc => "hij@yahoo.com",

use single-quotes around the right side:

my %mail = ( To => 'abc@yahoo.com',
From => 'def@yahoo.com',
Cc => 'hij@yahoo.com',

oterwise perl will think @yahoo is an array and you will end up with: abc.com instead of abc@yahoo.com

I already suggested you try MIME::Lite. Since I do not know what all is involved to construct a multipart/mixed email I have no idea what is wrong with your code besides the errors above.

Why can't you use MIME::Lite? If you can't install it you can use it more than likely use via the lib pragma:

use lib qw(/path/to/files)'
use MIME::Lite;

put the MIME::Lite source code in the /path/to/files folder and perl will find it.
-------------------------------------------------

 
 


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

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