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 2272
[愿得一人]
[愿得一人] 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:37

    this code working fine for me

        $mail = new PHPMailer;
        //Enable SMTP debugging. 
        $mail->SMTPDebug = 0;
        //Set PHPMailer to use SMTP.
        $mail->isSMTP();
        //Set SMTP host name                          
        $mail->Host = $hostname;
        //Set this to true if SMTP host requires authentication to send email
        $mail->SMTPAuth = true;
        //Provide username and password     
        $mail->Username = $sender;
        $mail->Password = $mail_password;
        //If SMTP requires TLS encryption then set it
        $mail->SMTPSecure = "ssl";
        //Set TCP port to connect to 
        $mail->Port = 465;
        $mail->From = $sender;  
        $mail->FromName = $sender_name;
        $mail->addAddress($to);
        $mail->isHTML(true);
        $mail->Subject = $Subject;
        $mail->Body = $Body;
        $mail->AltBody = "This is the plain text version of the email content";
        if (!$mail->send()) {
            echo "Mailer Error: " . $mail->ErrorInfo;
        }
        else {
               echo 'Mail Sent Successfully';
        }
    

提交回复
热议问题