Gmail SMTP凭据:无法发送邮件。邮件错误:SMTP错误:无法验证

时间:2020-08-18 20:49:42

标签: php email smtp phpmailer smtp-auth

当我在本地主机上运行代码时,工作得很完美,我收到了电子邮件,但是当我在Web主机Hostgator中运行时,它不起作用,我也不知道为什么

代码:

<?php

try {
//Server settings
$mail->SMTPDebug = false;                      // Enable verbose debug output
$mail->isSMTP();                                            // Send using SMTP
$mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
$mail->Username   = '*******@gmail.com';                     // SMTP username
$mail->Password   = '********';                               // SMTP password
$mail->SMTPSecure = 'tls';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

//Recipients
$mail->setFrom($mensagem->_get('email'), $mensagem->_get('nomear'));
$mail->addAddress('*******@gmail.com', '*********');     // Add a recipient
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

// Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

// Content
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = $mensagem->_get('assunto');
$mail->Body    = "O úsuario do site: <strong>{$mensagem->_get('nomear')}</strong> está entrando em contato com você pelo motivo: <strong>{$mensagem->_get('assunto')}</strong>, com a específica mensagem: <strong>{$mensagem->_get('msg')}</strong>. Responda para: {$mensagem->_get('email')}";
//$mail->AltBody = '';

$mail->send();

header('Location: obrigado.html');

} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

我已经检查了它是否是键入错误,但是如果是,则无法在我的本地主机上运行,​​并且我已经在我的Gmail上启用了权限。

1 个答案:

答案 0 :(得分:1)

该错误消息看起来很明显:您的登录凭据不正确。尽管如此,Gmail还是有点特别。

主机,端口和加密设置看起来还可以。也许您的密码已过期?

此外,尝试更改密码。 我听说如果Google认为凭证太简单,则凭证可能会被拒绝。

通常,尝试启用SMTP调试消息

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

..并检查:允许在您的Google帐户中启用较少的安全应用

登录到Google管理控制台/帐户设置,
转到安全性>基本设置。
转到不太安全的应用程序。启用“允许安全性较低的应用程序”。