繁体   English   中英

发送邮件 SMTP GMail 和 PHPMailer

[英]Send mail SMTP GMail and PHPMailer

我在使用 Google SMTP 和最新版本的 PHPMailer 发送邮件时遇到问题。 这里有很多问题,但答案对我不起作用。 我正在使用带有免费子域000webhost免费托管。 这是我从PHPMailer文档发送电子邮件的标准 PHP 代码:

$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'GMAIL EMAIL ACCOUNT';
$mail->Password = 'MY PASSWORD FOR GMAIL EMAIL ACCOUNT';
$mail->SMTPSecure = 'tlc';
$mail->Port = 587;
$mail->SMTPDebug  = 3;
$mail->setFrom('EMAIL ADDRESS', 'EXAMPLE');
$mail->addAddress('ADDRESS TO SEND', 'From contact form');

$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

我收到这个错误:

.... 2017-06-03 15:15:33 服务器 -> 客户端:535-5.7.8 用户名和密码不被接受。 在 535 5.7.8 https://support.google.com/mail/?p=BadCredentials 203sm11822739ywk.62 - gsmtp 2017-06-03 15:15:33 SMTP 错误:密码命令失败:535-5.7 了解更多信息。不接受用户名和密码。 在 535 5.7.8 https://support.google.com/mail/?p=BadCredentials 203sm11822739ywk.62 - gsmtp 2017-06-03 15:15:33 SMTP 错误:无法进行身份验证了解更多信息。 .....

如果我将ssl与端口465一起使用,我会看到同样的错误。

在我的Gmail电子邮件帐户中 2-factory authentication is OFFAllow less secure apps is ON

我真的不知道该怎么办。

我找到了一种创建Google App Password的方法,并使用它代替Gmail帐户密码:

$mail->Password = 'GOOGLE APP PASSWORD';

要创建应用密码,您需要设置Two factory authenticationhttps : //support.google.com/accounts/answer/185839?hl=en ,然后您可以创建应用密码 - https://support.google.com/帐户/答案/185833?hl=en

  1. https://myaccount.google.com 上登录您的 Google 帐户
  2. 单击左侧菜单中的“安全”。
  3. 向下滚动到“不太安全的应用程序访问”部分。

    单击“打开访问(不推荐)”。

https://support.google.com/mail/thread/23875320?hl=en

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM