Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^6: Using Net::SMTP to send pdf attachment

by Anonymous Monk
on Mar 16, 2018 at 15:57 UTC ( #1211056=note: print w/replies, xml ) Need Help??


in reply to Re^5: Using Net::SMTP to send pdf attachment
in thread Using Net::SMTP to send pdf attachment

Hi poj thanks. Here's the complete code:

use strict; use Net::SMTP; use MIME::Base64; my $smtphost = 'some_smtp_host'; my $username = 'username'; my $password = 'password'; my $emailto = 'to@hello.com'; my $emailfrom = 'from@hello.com'; my $subject = 'Hello world'; my $message = 'Test message'; my $smtp = Net::SMTP->new($smtphost, Debug => 1, Timeout => 5); $smtp->datasend("AUTH LOGIN\n"); $smtp->datasend(encode_base64($username)); $smtp->datasend(encode_base64($password)); $smtp->mail($emailfrom); $smtp->to($emailto); $smtp->data(); my $boundary = 'frontier'; my $attachBinaryFile = '18560243.pdf'; $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundar +y\"\n"); $smtp->datasend("--$boundary\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: appliaction/pdf; name=\"$attachBinaryFi +le\"\n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("Content-Disposition: attachment; filename=\"$attachBi +naryFile\"\n"); $smtp->datasend("\n"); my $buf; open(DAT, "/path/18560243.pdf") || 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("\n"); $smtp->datasend("--$boundary\n"); $smtp->dataend(); $smtp->quit; sub date_r { my ($monthday, $mon, $yr, $ time, $hour, $str); my (@lt) = (); @lt = localtime(); $monthday = $lt[3]; $mon = $lt[4]+1; $yr = $lt[5] + 1900; $hour = $lt[2]; $time = sprintf("%02d:%02d:%02d", $hour, $lt[1], $lt[0]); $str = $mon . '/' .$monthday . '/' . $yr . ' ' . $time; return $str; }

Replies are listed 'Best First'.
Re^7: Using Net::SMTP to send pdf attachment
by poj (Abbot) on Mar 16, 2018 at 17:43 UTC

    Changed to use a chunk/buffer that is a multiple of 57 bytes, corrected 'appliaction/pdf' and added some extra line endings. Much better to use a module if you can.

    #!perl use strict; use Net::SMTP; use MIME::Base64; my $smtphost = ''; my $username = ''; my $password = ''; my $emailto = ''; my $emailfrom = ''; my $subject = 'Hello world'; my $message = 'Test message'; my $smtp = Net::SMTP->new($smtphost, Debug => 1, Timeout => 5); $smtp->datasend("AUTH LOGIN\n"); $smtp->datasend(encode_base64($username)); $smtp->datasend(encode_base64($password)); #$smtp->auth($username,$password); $smtp->mail($emailfrom); $smtp->to($emailto); $smtp->data(); my $now = date_r(); $smtp->datasend("To: $emailto\n"); $smtp->datasend("From: $emailfrom\n"); $smtp->datasend("Date: $now\n"); $smtp->datasend("Subject: $subject\n"); my $boundary = 'frontier'; my $attachBinaryFile = '18560243.pdf'; $smtp->datasend("MIME-Version: 1.0\n"); #$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$bounda +ry\"\n"); $smtp->datasend("Content-type: multipart/mixed; boundary=\"$boundary\" +\n\n"); $smtp->datasend("--$boundary\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: appliaction/pdf; name=\"$attachBinaryF +ile\"\n"); $smtp->datasend("Content-Type: application/pdf; name=\"$attachBinaryFi +le\"\n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("Content-Disposition: attachment; filename=\"$attachBi +naryFile\"\n"); $smtp->datasend("\n"); my $buf; #open(DAT, "/path/$attachBinaryFile") || die("Could not open binary fi +le!"); open DAT, '<',"/path/$attachBinaryFile" or die "Could not open $attach +BinaryFile : $!"; binmode(DAT); local $/=undef; # while (read(DAT, my $picture, 4096)) { while (read(DAT, my $picture, 4104)) { $buf = &encode_base64( $picture ); $smtp->datasend($buf); } close(DAT); $smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); $smtp->dataend(); $smtp->quit; sub date_r { # my ($monthday, $mon, $yr, $ time, $hour, $str); # my (@lt) = (); # $monthday = $lt[3]; # $mon = $lt[4]+1; # $yr = $lt[5] + 1900; # $hour = $lt[2]; my @lt = localtime(); $lt[4] += 1; $lt[5] += 1900; my $str = sprintf "%02d/%02d/%04d %02d:%02d:%02d",@lt[4,3,5,2,1,0]; return $str; }
    poj

      Yayy! It's working now with the changes you made. Great Thanks !!!

      Thanks too for the improved time subroutine.

      Just out of curiosity, how does changing the chunk/buff to 4104 make the pdf file work? Will it work for all other pdf files created in similar fashion? Is 4104 a sort of magic number - would other numbers like 4105 or 4106 work too?

        The max encoded line length is 76. 3 bytes = 4 chars so 57 bytes = one full line. Any multiple of 3 avoids adding filler characters '=' to make up the encoded 4 characters and multiples of 57 give minimal line endings. 4095 also worked but 4104 is just the nearest multiple of 57 to 4096. See EXAMPLES in MIME::Base64

        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1211056]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2021-04-10 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?