首页 > 解决方案 > 我的代码中是否有任何问题“电子邮件无法使用我的 G-suite 帐户电子邮件”与 phpmailer

问题描述

2019-08-01 09:41:35 连接:关闭

SMTP 错误:无法验证。无法发送消息。邮件程序错误:SMTP 错误:无法验证。

$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host       = 'smtp.gmail.com';
$mail->SMTPAuth   = true;
$mail->Username   = '*******@mydomain.com';
$mail->Password   = '******'; 
$mail->SMTPSecure = 'ssl';                                 
$mail->Port       = 465;

标签: phpgmail

解决方案


            $mail = new PHPMailer(true);                             
            try {
                //Server settings

                $mail->SMTPDebug = 0;
                $mail->isSMTP();                                     
                $mail->Host = 'smtp.gmail.com';                      
                $mail->SMTPAuth = true;                               
                $mail->Username = 'test2@gmail.com';     
                $mail->Password = '123456';                    
                $mail->SMTPOptions = array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true
                    )
                );              
                $mail->SMTPSecure = 'ssl';                           
                $mail->Port = 465;
                $mail->IsHTML(true); // send as HTML             
                $mail->SMTPKeepAlive = true;  
                $mail->CharSet = 'utf-8';  

                $mail->setFrom('test@gmail.com');
                
                //Recipients
                $mail->addAddress($email);              
                $mail->addReplyTo('test@gmail.com');
               
                //Content
                $mail->isHTML(true);                                  
                $mail->Subject = 'Reset';
                $mail->Body    = $message;

                $mail->send();

                $_SESSION['success'] = 'Password reset link sent';
             
            } 
            catch (Exception $e) {
                $_SESSION['error'] = 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
            }

推荐阅读