I have following code to send an email in Perl:
#!/usr/bin/perl
use MIME::Lite;
$to = 'toid@domain.com';
$cc = 'ccid@domain.com';
$from = 'fromid@domain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Data => $message
);
$msg->send;
#$msg->send('smtp', "smtp.gmail.com", AuthUser=>"myid@domain.com", AuthPass=>"mypass" );
#$msg->send('smtp', "smtp.gmail.com", Debug=>0 );
#$msg->send('type',@args);
print "Email Sent Successfully\n";
When I run it I get the following error:
SMTP Failed to connect to mail server:
When I call $msg->send
with arguments (see the commented lines above) I get the following error:
SMTP auth() command not supported on smtp.gmail.com
How can I fix this?
Somebody filed a bug report for this several years ago. The maintainer's response was:
This is unlikely to be fixed.
MIME::Lite does not support Net::SMTP::TLS, and I don't see myself implementing that in the future. I strongly suggest moving off of MIME::Lite to tools like Email::Sender and Email::MIME or other more-supported tools.
Note that you shouldn't even be using MIME::Lite
in the first place, since the documentation recommends against it:
WAIT!
MIME::Lite is not recommended by its current maintainer. There are a number of alternatives, like Email::MIME or MIME::Entity and Email::Sender, which you should probably use instead. MIME::Lite continues to accrue weird bug reports, and it is not receiving a large amount of refactoring due to the availability of better alternatives. Please consider using something else.
MIME::Lite is (as ThisSuitIsNotBlack notes) deprecated.
This works for me, using the preferred Email::Sender:
use strict;
use warnings;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTPS ();
use Email::Simple ();
use Email::Simple::Creator ();
my $smtpserver = 'server';
my $smtpport = 587;
my $smtpuser = 'username';
my $smtppassword = 'password';
my $transport = Email::Sender::Transport::SMTPS->new({
host => $smtpserver,
port => $smtpport,
ssl => "starttls",
sasl_username => $smtpuser,
sasl_password => $smtppassword,
});
my $email = Email::Simple->create(
header => [
To => 'mymail@gmail.com',
From => 'sender@example.com',
Subject => 'Hi!',
],
body => "This is my message\n",
);
sendmail($email, { transport => $transport });
It can be fixed with Net::SMTP 3.05 (the latest version at CPAN). It supports SMTPS and STARTTLS.
[ WARNING: see MIME::Lite 3.030 - NET::SMTP with smtps (port 465) ]
# It should work with Net::SMTP 3.05
# MIME::Lite will pass SSL=>1 to Net::SMTP constructor
$msg->send('smtp', "smtp.gmail.com", SSL=>1,
AuthUser=>"myid@domain.com", AuthPass=>"mypass" );