我是新的PHP和试图建立一个网站与注册表格。用户应该收到一封带有确认链接的电子邮件(因为我有一个免费的主机服务器,我正在尝试使用gmail服务器)。问题是我在和电子邮件php代码做斗争。我试图按以下方式使用PHPMailer函数:
<?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "MYEMAIL@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "MYPASSWORD";
//Set who the message is to be sent from
$mail->setFrom('example@example.com', 'Name S');
//Set an alternative reply-to address
$mail->addReplyTo('example@example.com', 'Name S');
//Set who the message is to be sent to
$mail->addAddress('email@gmail.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("HELLo");
//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
?>
我试过587和465端口。还尝试了ssl和tls。每当我试图运行代码时,我都会得到以下错误:
服务器->客户端: 220 smtp.gmail.com ESMTP gw4sm17090153wjc.45-gsmtp客户端->服务器: EHLO spec.dr-manny.co.uk服务器->客户端: 250-smtp.gmail.com185.27.134.36250-大小为35882577250-8 browser 250-8月登录普通XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 XOAUTH2平原-CLIENTTOKEN OAUTHBEARER SMTPUTF8客户端->服务器: AUTH登录服务器->客户端: 334 VXNlcm5hbWU6客户端->服务器: bWFubnlzYWVkaUBnbWFpbC5jb20=服务器->客户端: 334 UGFzc3dvcmQ6客户端->服务器: a2luZ29uZW1vaDk5服务器->客户端: 534-5.7.14请通过web浏览器登录,然后再试534- 5.7.14了解更多的at534 5.7.14 https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 - gsmtp错误:密码命令失败: 534-5.7.14请通过web浏览器登录,534- 5.7.14然后再试。534-5.7.14了解更多at534 5.7.14 https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45-gsmtpsmtp错误:无法验证。客户端->服务器:退出服务器->客户端: 221 2.0.0关闭连接gw4sm17090153wjc.45 -连接()失败。错误: SMTP连接()失败。
另外,我收到了一封Gmail的电子邮件,主题是“有人有你的密码”。我打开邮件发现了这个“
嗨,曼尼,有人用你的密码试图登录你的谷歌账户MYEMAIL@gmail.com,使用的是电子邮件客户端或移动设备等应用程序。“我在运行php页面大约15分钟后收到了这封电子邮件。
我关闭了“两步验证”,并关闭了“允许不那么安全的应用程序”。似乎什么也帮不上忙。有人能帮我吗?
发布于 2016-07-29 17:03:54
去年我也遇到了同样的问题:代码(VB.Net)没有问题,所以凭证在哪里。问题是,gmail不喜欢在很远的web服务器(我的主机)中的某个应用程序,在那里尝试使用您的用户名。
我(在两次不同的场合)通过以下方式修正了这个问题:
在那之后再次测试你的代码。
https://stackoverflow.com/questions/38663156
复制相似问题