Home: Perl Programming Help: Beginner:
Issue with attaching 2 files in email using Perl Sendmail



zaxxon25
New User

Jul 20, 2008, 12:18 PM


Views: 679
Issue with attaching 2 files in email using Perl Sendmail

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


Views: 675
Re: [zaxxon25] Issue with attaching 2 files in email using Perl Sendmail

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.
-------------------------------------------------