in reply to Using Net::SMTP to send email attachments

Brother Monks

This humble initiate has finally got the script to send emails with an attachment! The final script (suitably anonymised) is given below.

I got the script to work OK on Compuserve (AOL) and on a Swiss email server with whom I have an account, but I could get nothing at all out of gmail, not even in the spam box. There was a clue, however. All emails sent to my Compuserve address are forwarded to my gmail account and there WAS a spam email which said a trial email I sent to the Compuserve email address, which did appear there and was forwarded to my gmail account, was being put into the spam box because of a suspected fake bounce. This is the cc line...

$smtp->datasend("Cc: info\@$company\n");

...so, I commented out this line (see below) and tried my gmail address again, and got instant success!

Anyhow, in the year of Our Lord 2017, I can commend this script to anyone who wants to use Net::SMTP to send an email with an attachment. This is a working script. (This is a cgi script in that it is being accessed via the web, so it has the appropriate cgi headers for that reason.)

Many thanks to all those who made suggestions on getting this script to work, in particular Huck, but others too whose suggestions or links provided enlightenment.

Geoffrey
#!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use warnings; use Net::SMTP; use MIME::Base64; my ($buf, $picture); my $company = 'my_company.com'; my $path = "/home/sites/$company/public_html"; my $attachBinaryFile = "image.jpg"; my $boundary = 'frontier'; my $passwd = "password"; my $contact = "name"; my $email = "somebody\@somewhere.com"; $smtp = Net::SMTP->new("mail.$company", Timeout => 30,Debug => 0,); $smtp->datasend("AUTH LOGIN\n"); $smtp->response(); $smtp->datasend(encode_base64("$contact\@$company") ); $smtp->response(); $smtp->datasend(encode_base64("$passwd") ); $smtp->response(); $smtp->mail("$contact\@$company"); $smtp->to($email); $smtp->cc(); $smtp->data(); $smtp->datasend("To: $email\n"); $smtp->datasend("From: $contact\@$company\n"); $smtp->datasend("Subject: Try-out of email script\n"); #$smtp->datasend("Cc: info\@$company\n"); $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundar +y\"\n"); #$smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); #$smtp->datasend("Content-type: text/plain; charset=\"UTF-8\"\n"); $smtp->datasend("Content-type: text/plain;\n"); $smtp->datasend("\nSome plain text here in the body of the email\n"); $smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-Type: image/jpeg; name=\"$attachBinaryFile\"\ +n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("Content-Disposition: attachment; filename=\"$attachBi +naryFile\"\n"); $smtp->datasend("\n"); open(DAT, "$path/$attachBinaryFile") || die("Could not open binary fil +e!"); binmode(DAT); local $/=undef; while (read(DAT, $picture, 4096)) { $buf = &encode_base64( $picture ); $smtp->datasend($buf); } close(DAT); $smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); $smtp->dataend(); $smtp->quit; print "Mail sent\n"; exit; print "</body></html>";

Replies are listed 'Best First'.
Re^2: Using Net::SMTP to send email attachments
by afoken (Chancellor) on May 02, 2017 at 19:07 UTC
    $smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); $smtp->dataend(); $smtp->quit;

    This is still wrong, and might work only accidentally. The last boundary must be followed by --. See Re^5: Using Net::SMTP to send email attachments.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)