Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an Net::SMTP email in perl

Tags:

email

smtp

perl

None of my hosts have any higher email modules, so I'd prefer to get this working without additional modules.

I've tried using google smtp servers (which i got working with \sendmail in XAMPP but never through Net::SMTP), I've tried secureserver.net smtp servers. with and without SSL, with and without Hello lines, port 465 or 25. Tried running from my local XAMPP server disabled all firewalls, tried running it remotely on goDaddy server.

Seems to be timing out and 'smtp failed'. never getting to '...' even without the unless($smtp) test. failing to get past auth, but since 'smtp failed' is being fired i assume its a problem with how i am forming the initial declaration of the object? Though I've tried this in several different ways and I'm not getting any actual server errors.

my $smtp = Net::SMTP->new($smtpserver,
    SSL => 1,                   
    Timeout => 60,
    Port => $smtpport,  # tried 25 and 465
    Debug => 1
);
    unless($smtp) { #tried with and without this
        print "smtp failed<br>[".$!."]<br>and<br>".$IO::Socket::SSL::SSL_ERROR;
    }
$smtp->auth($smtpuser, $smtppassword);    #nothing under this line is done
$smtp->mail($smtpuser);            #but i have checked and double checked the data
$smtp->recipient($recipi);         #pointed to several email addys but never arrive
$smtp->data();
$smtp->datasend("From: $smtpuser");
$smtp->datasend("To: $recipi");
$smtp->datasend("Subject: test mail ver six sm loc 12"); 
$smtp->datasend("\n");
$smtp->datasend("blah blah");
$smtp->dataend();
$smtp->quit;
print"...";  #never gets printed

Any suggestions welcome, the only thing I've thought of and not tried is including the authentication in the initial object deceleration, i can find no where that shows how to do that.

edit; added $! and $IO::Socket::SSL::SSL_ERROR Steffen suggested the result is $! = [Bad file descriptor] and $IO::Socket::SSL::SSL_ERROR returns nothing.

like image 683
jobeSW Avatar asked Nov 06 '25 03:11

jobeSW


2 Answers

Try the following code (it requires Authen::SASL module which might be not part of your setup)

use strict;
use warnings;
use utf8;

use Net::SMTP 3.0;
use Authen::SASL qw(Perl);

my $sender = my $user = '[email protected]';
my $password = 'your_password';
my $receiver = '[email protected]';

my $smtp = Net::SMTP->new(
    'smtp.gmail.com',
    SSL=>1,
    Timeout => 20,
    Debug   => 1,
);

die "Initialization failed: $!" if !defined $smtp;

print "Trying to authenticate..";
$smtp->auth( $user, $password) or die "could not authenticate\n";

my $header = 
"To: $receiver
From: $sender
Content-Type: text/html
Subject: Testing Net::SMTP

";

my $body = "
The <i>body</i> of the <b>email</b> Net-SMTP example
";

$smtp->mail( $sender );
$smtp->to( $receiver );
$smtp->data();
$smtp->datasend( $header );
$smtp->datasend( $body );
$smtp->datasend( '' );
$smtp->dataend();
$smtp->quit();
like image 71
Polar Bear Avatar answered Nov 09 '25 01:11

Polar Bear


Authen::SASL (last updated 2012) seems to be not needed these days. I'm not sure why.

This code sufficed to send to Gmail:

use v5.32;
use warnings;
use Net::SMTP;

my %smtp = (
  host => 'smtp.gmail.com',
  from     => '', # also: user
  password => q(),     # app password: https://support.google.com/accounts/answer/185833
  to       => '',
  subject  => 'Net::SMTP: Hello',
);

my $smtp = Net::SMTP->new($smtp{host},
  SSL   => 1,
  Debug => 1,
);

$smtp->auth($smtp{from}, $smtp{password});;

my $header = <<"HEADER";
To: $smtp{to}
From: $smtp{from}
Content-Type: text/html
Subject: $smtp{subject}
HEADER

my $body = <<"BODY";
The <i>body</i> of the <b>email</b> Net-SMTP example
BODY

$smtp->mail($smtp{from}); # SMTP: MAIL FROM
$smtp->to($smtp{to}); # SMTP: RCPT TO

$smtp->data(); # SMTP: DATA
$smtp->datasend($header);
$smtp->datasend($body);
$smtp->dataend(); # SMTP: .

$smtp->quit(); # SMTP: QUIT

This is a modification of the previous answer with method-explaining comments, and without excessive code such as the SSL module and datasend with empty string.

like image 43
Elvin Avatar answered Nov 09 '25 00:11

Elvin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!