Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix?

前端 未结 12 2249
[愿得一人]
[愿得一人] 2020-11-22 02:42

I would like to send an email using Gmail SMTP server through PHP Mailer.

this is my code



        
相关标签:
12条回答
  • 2020-11-22 03:12

    So I just solved my own "SMTP connection failure" error and I wanted to post the solution just in case it helps anyone else.

    I used the EXACT code given in the PHPMailer example gmail.phps file. It worked simply while I was using MAMP and then I got the SMTP connection error once I moved it on to my personal server.

    All of the Stack Overflow answers I read, and all of the troubleshooting documentation from PHPMailer said that it wasn't an issue with PHPMailer. That it was a settings issue on the server side. I tried different ports (587, 465, 25), I tried 'SSL' and 'TLS' encryption. I checked that openssl was enabled in my php.ini file. I checked that there wasn't a firewall issue. Everything checked out, and still nothing.

    The solution was that I had to remove this line:

    $mail->isSMTP();
    

    Now it all works. I don't know why, but it works. The rest of my code is copied and pasted from the PHPMailer example file.

    0 讨论(0)
  • 2020-11-22 03:14

    Also worth noting that if you have two factor authentication enabled, you'll need to setup an application specific password to use in place of your email account's password.

    You can generate an application specific password by following these instructions: https://support.google.com/accounts/answer/185833

    Then set $mail->Password to your application specific password.

    0 讨论(0)
  • 2020-11-22 03:14

    I think it is connection issue you can get code here http://skillrow.com/sending-mail-using-smtp-and-php/

    include(“smtpfile.php“);
    include(“saslfile.php“); // for SASL authentication $from=”my@website.com“; //from mail id
    
    $smtp=new smtp_class;
    
    $smtp->host_name=”www.abc.com“; // name of host
    $smtp->host_port=25;//port of host
    
    $smtp->timeout=10;
    $smtp->data_timeout=0;
    $smtp->debug=1;
    $smtp->html_debug=1;
    $smtp->pop3_auth_host=””;
    $smtp->ssl=0;
    $smtp->start_tls=0;
    $smtp->localhost=”localhost“;
    $smtp->direct_delivery=0;
    
    $smtp->user=”smtp username”;
    $smtp->realm=””;
    $smtp->password=”smtp password“;
    
    $smtp->workstation=””;
    $smtp->authentication_mechanism=””;
    
    $mail=$smtp->SendMessage($from,array($to),array(“From:$from”,”To: $to”,”Subject: $subject”,”Date: ”.strftime(“%a, %d %b %Y %H:%M:%S %Z”)),”$message”);
    
    if($mail){
       echo “Mail sent“;
    }else{
       echo $smtp->error;
    }
    
    0 讨论(0)
  • 2020-11-22 03:19

    hello to everyone just wanted to share my experience with phpMailer , that was working locally (XAMPP) but wasn't working on my hosting provider.

    I turned on phpMailer error reporting

     $mail->SMTPDebug=2
    

    i got 'Connection refused Error'

    I email my host provider for the issue , and he said that he would open the SMTP PORTS and he opened the ports 25,465,587 .

    Then i got the following error response "SMTP ERROR: Password command failed:"...."Please log in via your web browser and then try again"...."SMTP Error: Could not authenticate."

    So google checks if your are logged in to your account (i was when i ran the script locally through my browser) and then allows you to send mail through the phpMailer script.

    To fix that 1:go to your google account -> security 2:Scroll to the Key Icon and choose "2 way verification" and follow the procedure 3:When done go back to the key icon from google account -> security and choose the second option "create app passwords" and follow the procedure to get the password.

    Now go to your phpMailer object and change your google password with the password given from the above procedure

    you are done .

    The code

    require_once('class.phpmailer.php');
    
    $phpMailerObj= new PHPMailer();
    
                    $phpMailerObj->isSMTP();                    
                    $phpMailerObj->SMTPDebug = 0;
                    $phpMailerObj->Debugoutput = 'html';                    
                    $phpMailerObj->Host = 'smtp.gmail.com';                     
                    $phpMailerObj->Port = 587;
                    $phpMailerObj->SMTPSecure = 'tls';
                    $phpMailerObj->SMTPAuth = true;                 
                    $phpMailerObj->Username = "YOUR EMAIL";                 
                    $phpMailerObj->Password = "THE NEW PASSWORD FROM GOOGLE ";
                    $phpMailerObj->setFrom('YOUR EMAIL ADDRESS', 'THE NAME OF THE SENDER',0);
                    $phpMailerObj->addAddress('RECEIVER EMAIL ADDRESS', 'RECEIVER NAME');
    
                    $phpMailerObj->Subject = 'SUBJECT';
                    $phpMailerObj->Body ='MESSAGE';
    
                    if (!phpMailerObj->send()) {
                        echo "phpMailerObjer Error: " . $phpMailerObj->ErrorInfo;
                        return 0;
                    } else {
                        echo "Message sent!";
                        return 1;
                    } 
    
    0 讨论(0)
  • 2020-11-22 03:25

    It seems that your server fails to establish a connection to Gmail SMTP server. Here are some hints to troubleshoot this: 1) check if SSL correctly configured on your PHP (module that handle it isn't installed by default on PHP. You have to check your configuration in phph.ini). 2) check if your firewall let outgoing calls to the required port (here 465 or 587). Use telnet to do so. If the port isn't opened, you'll then require some support from sysdmin to setup the config. I hope you'll sort this out quickly!

    0 讨论(0)
  • 2020-11-22 03:27
    $mail = new PHPMailer(); // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; // or 587
    $mail->IsHTML(true);
    $mail->Username = "email@gmail.com";
    $mail->Password = "password";
    $mail->SetFrom("example@gmail.com");
    $mail->Subject = "Test";
    $mail->Body = "hello";
    $mail->AddAddress("email@gmail.com");
    
     if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
     } else {
        echo "Message has been sent";
     }
    

    This code above has been tested and worked for me.

    It could be that you needed $mail->SMTPSecure = 'ssl';

    Also make sure you don't have two step verification switched on for that account as that can cause problems also.

    UPDATED

    You could try changing $mail->SMTP to:

    $mail->SMTPSecure = 'tls';
    

    It's worth noting that some SMTP servers block connections. Some SMTP servers don't support SSL (or TLS) connections.

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