Why do I get “SMTP Failed to connect to mail server:” when I try to send an email to a Gmail account using MIME::Lite?

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

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?

回答1:

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.



回答2:

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 }); 


回答3:

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" ); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!