Connecting Tech Pros Worldwide Forums | Help | Site Map

Attaching 2 files to email using perl sendmail

Newbie
 
Join Date: Jul 2008
Posts: 2
#1: Jul 20 '08
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.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use Mail::Sendmail;
  4. use MIME::QuotedPrint;
  5. use MIME::Base64;
  6.  
  7. my $file1 = 'c:\test1.xls';
  8. my $file2 = 'c:\sample.zip';
  9.  
  10.  
  11. my $file3 = $file2 || $file1; # file to attach
  12.  
  13. my %mail = ( To   => "abc@yahoo.com",
  14.               From    => "def@yahoo.com",
  15.               Cc      => "hij@yahoo.com",
  16.               Subject => "Two Attachments",
  17.               smtp    => 'mac.abc.com'
  18.              );
  19.  
  20. my $content;
  21. { local $/ = undef; # slurp file
  22. open IN, $file3 or die "Error opening $file3: $!";
  23. binmode IN; $content = <IN>; 
  24. $mail{body} += encode_base64(<IN>);
  25. close IN;
  26. }
  27.  
  28.  
  29. my $boundary = "====" . time() . "====";
  30. $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
  31.  
  32. my $message = encode_qp( $email_body );
  33.  
  34. my $len = length $$mail{body};
  35. $mail{body} = <<EOD;
  36.  
  37.  
  38. $boundary
  39. Content-Type: text/plain; charset="iso-8859-1"
  40. Content-Transfer-Encoding: quoted-printable
  41.  
  42. $message
  43. $boundary
  44.  
  45. Content-Type: application/octet-stream; name="$file3"
  46. Content-Disposition: attachment; filename="$file3"
  47. Content-Transfer-Encoding: base64
  48. Content-Length: $len
  49.  
  50. $mail{body}
  51. --$boundary
  52. Content-Type: application/vnd.ms-excel; name="$file3"
  53. Content-Disposition: attachment; filename="$file3"
  54. Content-Transfer-Encoding: base64
  55. Content-Length: $len
  56.  
  57. $mail{body}
  58. $boundary--
  59. END_OF_BODY
  60.  
  61.  
  62.  
  63. sendmail(%mail) or die $Mail::Sendmail::error;
  64.  
  65. open(OUT, ">>$sendmail_log") or die "Cannot open LOG file $file3: $!";
  66. print OUT $Mail::Sendmail::log;
  67. print OUT "\n==============================================================================\n";
  68. close(OUT);
  69.  

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Jul 20 '08

re: Attaching 2 files to email using perl sendmail


Instead of trying to debug your code I am going to suggest you use the MIME::Lite module to send the email, it will make adding attachments to your emails a simple process.

http://search.cpan.org/~rjbs/MIME-Li...b/MIME/Lite.pm
Newbie
 
Join Date: Jul 2008
Posts: 2
#3: Jul 20 '08

re: Attaching 2 files to email using perl sendmail


thanks Kevin but unfortunately i cannot use mime lite. we are doing development on windows server and it does not have mime lite module. we cannot install due to permission issue.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Jul 20 '08

re: Attaching 2 files to email using perl sendmail


see perlguru forum.
Reply