bobinyec has asked for the wisdom of the Perl Monks concerning the following question:

From what I've read, SSLGLUE is supposed to fix the "invalid SSL_version" error I've been getting. However, just including it doesn't help. The example is:

use Net::SSLGlue::SMTP; my $smtp_ssl = Net::SMTP->new( $host, SSL => 1, SSL_ca_path => ... );

I don't see how I'm supposed to use the variable $smtp_ssl and I don't know what the path (on Windows) to the certificates is.

Can someone please give a comprehensive example of how to send an email in perl on Windows 7?

Replies are listed 'Best First'.
Re: Sending Email On Windows
by roho (Bishop) on Apr 10, 2023 at 20:05 UTC
    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."

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

Re: Sending Email On Windows
by karlgoethebier (Abbot) on Apr 10, 2023 at 11:22 UTC
Re: Sending Email On Windows
by Anonymous Monk on Apr 09, 2023 at 13:40 UTC