roho has asked for the wisdom of the Perl Monks concerning the following question:
I must use module Net::SMTPS because it has the options required to connect to the Outlook web email server. I have not found another module that allows me to connect to the Outlook web email server.
I know there are a boatload of email modules that are higher level than Net::SMTPS (and I have tried many of them so far), but like I said, Net::SMTPS is the only module I have found with the options to allow me to connect to the Outlook web email server.
To run the code below, replace "tester@hotmail.com" with a valid Outlook web email userid, replace "foo" with a valid email password, and replace "test.jpg" with an existing JPG file.
Can anyone provide the additional code required to embed the JPG picture in the body of the email using Net::SMTPS? Thanks in advance for your help.
#!/usr/bin/perl use strict; use warnings; use Net::SMTPS; use MIME::Base64 qw( encode_base64 ); my $from = 'Tester <tester@hotmail.com>'; my $to = shift || 'tester@hotmail.com'; my $pic = shift || 'test.jpg'; # Email connection. my $username = 'tester@hotmail.com'; my $password = 'foo'; my $smtp = Net::SMTPS->new('outlook.com', Port => 587, doSSL => 'star +ttls', SSL_version=>'TLSv1'); $smtp->auth ( $username, $password ) or die "Could not authenticate wi +th Outlook.\n"; print "Sending mail\n"; # Email header. my $boundary = 'frontier'; $smtp->mail($from); $smtp->recipient($to, { SkipBad => 1 }); $smtp->data(); $smtp->datasend("To: $to\n"); $smtp->datasend("From: $from\n"); $smtp->datasend("Subject: Perl Test Email\n"); $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundar +y\"\n"); $smtp->datasend("\n"); # Email body. $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-type: text/plain\n"); $smtp->datasend("Content-Disposition: quoted-printable\n"); $smtp->datasend("Testing Perl email.\n"); # Email attachment. $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-Type: image/jpeg; name=\"$pic\"\n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("Content-Disposition: attachment; filename=\"$pic\"\n" +); $smtp->datasend("\n"); open(my $fh2, '<', $pic) || die("Could not open Jpg file!"); binmode($fh2); local $/=undef; while (read($fh2, my $chunk, 72*57)) { my $buf = &encode_base64( $chunk ); $smtp->datasend($buf); } close($fh2); $smtp->datasend("\n"); $smtp->datasend("--$boundary--\n"); $smtp->dataend(); $smtp->quit; print "Mail sent\n"; exit;
"It's not how hard you work, it's how much you get done."
|
|---|