in reply to Sending Email On Windows

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

Replies are listed 'Best First'.
Re^2: Sending Email On Windows
by bobinyec (Acolyte) on Apr 12, 2023 at 18:51 UTC
    my $smtp = Net::SMTPS->new('outlook.com', Port => 587, doSSL => 'star +ttls', SSL_version=>'TLSv1');

    This isn't working for me. The error I get is:

    SSL connect attempt failed because of handshake problems

    My smtp server is supposed to be using TLS 1.2, so I'm using:

    my $smtp = Net::SMTPS->new('outlook.com', Port => 587, doSSL => 'star +ttls', SSL_version=>'TLSv1_2');

    but this causes $smtp to be undefined.

      The value of the doSSL key, in the post to which you replied, was 'starttls'.

      When you viewed this it wrapped, leaving one line ending with 'star, and the next line starting with a + (probably red unless you've changed your settings) to indicate wrapping, which was followed by the remainder of the value, i.e. ttls'.

      As you have this in two code blocks, it does not appear to be a one-off typo. In both cases, you should have:

      ... doSSL => 'starttls', ...

      You should not have:

      ... doSSL => 'star +ttls', ...

      I cannot comment on the validity of the parameters to Net::SMTPS->new(...); however, blindly copying that + is definitely wrong.

      Edit (for clarity): s/on two lines/in two code blocks/

      — Ken

      Ken's comments are correct. The plus sign for line continuation is not part of the original line. Regarding the particulars of your email server, I had to do a lot of research to formulate the correct parameters for Outlook. You must do the same for the email server you are using. I'm afraid I cannot help you there.

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