merrymonk has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use Net::SMTP; use MIME::Base64 qw( encode_base64 ); my ($host_em, $to_em, $cc_em, $from_em, $password_em, @to_email_list, +$jt, $jt_max, $smtp, $attachBinaryFile, @attach_list, $ja, $ja_max, $ +buf); my ($content_type, $plength, $command_ok, $auth_ok); # host name required $host_em = 'mail.xxxx'; # valid e-mail address from which e-mail is to be sent and password fo +r this e-mail address required $from_em = 'xxx@xxx'; $password_em = 'xxxx'; # valid e-mail address to which e-mail is to be sent $to_email_list[0] = 'yyy@yyy'; # list of attachments for the e-mail $attach_list[0] = 'Bach.jpg'; $attach_list[1] = 'workshop notes.pdf'; $attach_list[2] = 'Booking form.pdf'; $attach_list[3] = 'testaaa.xls'; $attach_list[4] = 'testx.xlsx'; $attach_list[5] = 'wordtest.docx'; $ja_max = scalar(@attach_list); my $boundary = 'frontier'; $jt_max = scalar(@to_email_list); for($jt = 0; $jt < $jt_max; $jt ++) { $to_em = $to_email_list[$jt]; $smtp = Net::SMTP->new($host_em, SSL => 1); # test that auth is OK $auth_ok = $smtp->auth($from_em, $password_em); print "jt <$jt> command_ok <$auth_ok>\n"; $smtp->mail($from_em); $smtp->to($to_em); $smtp->data; $smtp->datasend("From: " . $from_em); $smtp->datasend("\n"); $smtp->datasend("To: " . $to_em); $smtp->datasend("\n"); $smtp->datasend('Subject: Test of e-mail list'); $smtp->datasend("\n"); $smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$bou +ndary\"\n"); $smtp->datasend("\n"); for($ja = 0; $ja < $ja_max; $ja ++) { $attachBinaryFile = $attach_list[$ja]; if ( $attachBinaryFile =~ /\.gif$/i ){ $content_type ='image/gif'} if ( $attachBinaryFile =~ /\.jpg$/i ){ $content_type ='image/jpeg' +} if ( $attachBinaryFile =~ /\.zip$/i ){ $content_type ='application +/zip'} if ( $attachBinaryFile =~ /\.html$/i ){ $content_type ='text/html' +} if ( $attachBinaryFile =~ /\.pdf$/i ){ $content_type ='application +/pdf'} if ( $attachBinaryFile =~ /\.xls$/i ){ $content_type ='application +/vnd.ms-excel'} if ( $attachBinaryFile =~ /\.doc$/i ){ $content_type ='application +/vnd.ms-word'} if ( $attachBinaryFile =~ /\.log$/i ){ $content_type ='application +/octet-stream'} $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-Type: $content_type; name=\"$attachBinary +File\"\n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("Content-Disposition: attachment; filename=\"$atta +chBinaryFile\"\n"); $smtp->datasend("\n"); print "$ja <$ja> attachment <$attachBinaryFile> type <$content_typ +e>\n"; $buf = '';; open(DAT, "$attachBinaryFile") || die("Could not open binary file! +"); binmode(DAT); local $/=undef; while (read(DAT, my $picture, 4096)) { $buf = &encode_base64( $picture ); $smtp->datasend($buf); } } close(DAT); $smtp->datasend("--$boundary\n"); # test that e-mail has been sent ok $command_ok = $smtp->dataend(); print "jt <$jt> command_ok <$command_ok>\n"; $smtp->quit; } print "\n\n\nFinished\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sending Multi file types as e-mail attachments
by Athanasius (Cardinal) on Nov 16, 2015 at 03:36 UTC | |
by merrymonk (Hermit) on Nov 16, 2015 at 08:33 UTC | |
by merrymonk (Hermit) on Nov 16, 2015 at 08:51 UTC | |
by merrymonk (Hermit) on Nov 16, 2015 at 09:01 UTC | |
|
Re: Sending Multi file types as e-mail attachments
by Corion (Patriarch) on Nov 16, 2015 at 09:04 UTC | |
|
Re: Sending Multi file types as e-mail attachments
by soonix (Chancellor) on Nov 16, 2015 at 19:54 UTC |