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?

前端 未结 4 1499
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 18:30

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@doma         


        
相关标签:
4条回答
  • 2020-12-19 18:33

    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 });
    
    0 讨论(0)
  • 2020-12-19 18:51

    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.

    0 讨论(0)
  • 2020-12-19 18:51

    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" );
    
    0 讨论(0)
  • 2020-12-19 18:55

    The above worked when

    1. I used my Gmail as the SMTP --> Bet you didn't think you could do that.
    my $smtpserver  = 'smtp.gmail.com.';
    my $smtpport     = 587;
    my $smtpuser     = 'YourGmailHere@gmail.com';
    my $smtppassword = 'password'; ## Plug in your password here
    
    1. Then you have to actually set your security for your Gmail account to a less secure … basically set "Less secure app access" to "ON" setting --> https://myaccount.google.com/security

    Hope this helps others.

    0 讨论(0)
提交回复
热议问题