in reply to Re: Sending Multi file types as e-mail attachments
in thread Sending Multi file types as e-mail attachments

Thank you - I appreciate your suggestions. Below is modified version where I have implemented using a hash for the content_type. This fails when the content type is not 'known'.
Does anyone know where I can find what the content type description should be for:
1. in general - any file type;
2. specifically - for ms word and excel having extensions .docx and .xlsx respectively?
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, %file_content, $fi +le_split_tot, @file_split); # set the file contant definition $file_content{gif} ='image/gif'; $file_content{jpg} ='image/jpeg'; $file_content{zip} ='application/zip'; $file_content{html} ='text/html'; $file_content{pdf} ='application/pdf'; $file_content{xls} ='application/vnd.ms-excel'; $file_content{doc} ='application/vnd.ms-word'; $file_content{log} ='application/octet-stream'; # 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]; # find file type @file_split = split(/\./, $attachBinaryFile); $file_split_tot = scalar(@file_split); if(exists($file_content{$file_split[$file_split_tot - 1]})) { $content_type = $file_content{$file_split[$file_split_tot +- 1]}; print "ext <$file_split[$file_split_tot - 1] content_type +<$content_type>\n"; $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-Type: $content_type; name=\"$atta +chBinaryFile\"\n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("Content-Disposition: attachment; filename +=\"$attachBinaryFile\"\n"); $smtp->datasend("\n"); print "$ja <$ja> attachment <$attachBinaryFile> type <$con +tent_type>\n"; $buf = '';; open(DAT, "$attachBinaryFile") || die("Could not open bina +ry file!"); binmode(DAT); local $/=undef; while (read(DAT, my $picture, 4096)) { $buf = &encode_base64( $picture ); $smtp->datasend($buf); } $smtp->datasend("--$boundary\n"); close(DAT); $smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); } else { print "no valid content for $file_split[$file_split_tot - +1] is available\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^3: Sending Multi file types as e-mail attachments
by merrymonk (Hermit) on Nov 16, 2015 at 08:51 UTC
    There was an error with the position of one of the } at the end of the Perl that I sent in my reply above.
    Below is the complete Perl again since I thought it would be simpler this way.
    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, %file_content, $fi +le_split_tot, @file_split); # set the file contant definition $file_content{gif} ='image/gif'; $file_content{jpg} ='image/jpeg'; $file_content{zip} ='application/zip'; $file_content{html} ='text/html'; $file_content{pdf} ='application/pdf'; $file_content{xls} ='application/vnd.ms-excel'; $file_content{doc} ='application/vnd.ms-word'; $file_content{log} ='application/octet-stream'; # 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]; # find file type @file_split = split(/\./, $attachBinaryFile); $file_split_tot = scalar(@file_split); if(exists($file_content{$file_split[$file_split_tot - 1]})) { $content_type = $file_content{$file_split[$file_split_tot +- 1]}; print "\n$ja <$ja> attachment <$attachBinaryFile> ext <$fi +le_split[$file_split_tot - 1] content_type <$content_type>\n"; $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-Type: $content_type; name=\"$atta +chBinaryFile\"\n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("Content-Disposition: attachment; filename +=\"$attachBinaryFile\"\n"); $smtp->datasend("\n"); $buf = '';; open(DAT, "$attachBinaryFile") || die("Could not open bina +ry file!"); binmode(DAT); local $/=undef; while (read(DAT, my $picture, 4096)) { $buf = &encode_base64( $picture ); $smtp->datasend($buf); } $smtp->datasend("--$boundary\n"); close(DAT); $smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); } else { print "no valid content for $file_split[$file_split_tot - +1] is available\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";
      I have found an answer to my last question.
      The site http://help.dottoro.com/lapuadlp.php
      amongst others has a list.
      .. but I am still looking for how to add the options