The code below sends an email with a JPG file as an attachment using the Outlook web email server. I need to send the email with the JPG picture embedded in the body of the email as well.
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."
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.