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."


In reply to Send Email With Picture Embedded in Body of Email (SOLVED) by roho

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.