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 2412
[愿得一人]
[愿得一人] 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: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.

提交回复
热议问题