The following code sends an email with text and an embedded image. Modify to suit your requirements. It is set up to use Microsoft Outlook email server. Modify "Net::SMTPS->new(... " with your email server particulars.

Special Note: In this sample code, the sender's username and password are assigned to variables. Do NOT, I repeat, do NOT, leave plain text passwords in your code (unless you enjoy having your email hacked).

#!/usr/bin/perl use strict; use warnings; use Net::SMTPS; use MIME::Lite; use MIME::Base64 qw( encode_base64 ); my $to = 'email@example.com'; my $from = 'email@hotmail.com'; my $subject = 'Sample Subject'; my $file = 'C:\temp\sample.jpg'; my $username = 'MyEmail@hotmail.com'; my $password = 'MyPassword'; # Warning: Do NOT leave plain text passw +ord in your code! 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"; ###################################################################### # 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: $subject\n"); $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundar +y\"\n"); $smtp->datasend("\n"); ###################################################################### # Email - text plus image embedded in email body. ###################################################################### $smtp->datasend("--$boundary\n"); my $msg = MIME::Lite->new( Type =>'multipart/related' ); $msg->attach( Type => 'text/html', Data => qq{ <body> <b>Email text (bold)</b> <br>Email text (normal) <p> <img src="cid:mypicture"> </body> }, ); $msg->attach( Type => 'image/jpeg', Id => 'mypicture', Path => $file, ); $smtp->datasend( $msg->as_string ); ###################################################################### # Email - finish. ###################################################################### $smtp->datasend("\n"); $smtp->datasend("--$boundary--\n"); $smtp->dataend(); $smtp->quit; exit;

"It's not how hard you work, it's how much you get done."


In reply to Re: Sending Email On Windows by roho
in thread Sending Email On Windows by bobinyec

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.