Pun*_*thi 52 php email smtp phpmailer
我已经使用phpmailer()概念使用php脚本从我的共享服务器向用户发送邮件,但是根据phpmailer代码,即使我的脚本中的所有内容都正确,我也无法发送.
我的代码是这样的:
$message = " This is testing message from my server";
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "moorthi.mrk10@gmail.com"; // My gmail username
$mail->Password = "************"; // My Gmail Password
$mail->SetFrom("moorthi.mrk10@gmail.com");
$mail->Subject = "Test Mail from my Server";
$mail->Body = $message;
$mail->AddAddress($email);
if($mail->Send())
{
print json_encode("SUCCESS");
}
else
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
Run Code Online (Sandbox Code Playgroud)
注意: 我使用"GMail"作为我的SMTP服务器,SMTPSecure是"ssl",端口是"465",用户名和密码是我的GMail用户名和密码
我使用VPS共享服务器,并将我的PHP脚本保存在该服务器上.
我认为我的PHP脚本没有问题,我不知道为什么它不起作用.
我得到了这样的错误.
2014-02-21 12:30:11 CLIENT -> SERVER: EHLO jkcorporates.com
2014-02-21 12:30:11 CLIENT -> SERVER: AUTH LOGIN
2014-02-21 12:30:11 CLIENT -> SERVER: bW9vcnRoaS5tcmsxMEBnbWFpbC5jb20=
2014-02-21 12:30:11 CLIENT -> SERVER: OTk0MTI0MTE0MA==
2014-02-21 12:30:11 SMTP ERROR: Password command failed: 534-5.7.14
534-5.7.14 i-_eumA> Please log in via your web browser and then try again.
534 5.7.14 54 k76sm17979938yho.18 - gsmtp
2014-02-21 12:30:11 CLIENT -> SERVER: QUIT
" The ERROR is " SMTP connect() failed.
Run Code Online (Sandbox Code Playgroud)
请给出一些解决方案.
提前致谢.
请记住:我使用共享服务器名称'VPS.mydomain.com',我想使用GMail作为我的SMTP服务器向用户发送邮件.
cal*_*bob 168
有点晚了,但也许有人会发现它很有用.
解决问题的链接(您必须登录到Google帐户):
https://security.google.com/settings/security/activity?hl=en&pli=1
https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha
对发生的事情的一些解释:
这个问题可能是由于"不太安全"的应用程序试图使用电子邮件帐户造成的(这是根据谷歌的帮助,不知道他们如何判断什么是安全的,什么不是)或者如果你试图在一个时间点登录行或者如果你改变国家(例如使用VPN,将代码移动到不同的服务器或实际尝试从世界的不同地方登录).
解决我不得不:(第一次)
这是第一次工作,但几个小时后,可能是因为我正在做很多测试再次出现的问题并且使用上述方法无法修复.此外,我必须清除验证码(有趣的图片,要求您在现在登录任何帐户时重写一个单词或一个句子太多次):
希望这可以帮助.
San*_*nex 22
用这个:
https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha
https://security.google.com/settings/security/activity?hl=en&pli=1
Run Code Online (Sandbox Code Playgroud)
此链接允许访问谷歌帐户
更新19-05-2017:
您必须从将要发送电子邮件的IP地址访问这些URL
小智 7
解决了问题 - PHPMailer - SMTP ERROR:从我的服务器发送邮件时密码命令失败
require_once('class.phpmailer.php');
include("class.smtp.php");
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$messageField = $_POST['message'];
$phoneField = $_POST['contactno'];
$cityField = $_POST['city'];
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
$body .= $nameField;
try {
//$mail->Host = "mail.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail->Username = "xxxxx@gmail.com"; // GMAIL username
$mail->Password = "********"; // GMAIL password
$mail->AddAddress('sendto@gmail.com', 'abc');
$mail->SetFrom('xxxxxx@gmail.com', 'def');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->Send();
echo "Message Sent OK</p>\n";
header("location: ../test.html");
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Run Code Online (Sandbox Code Playgroud)
重要:
转到谷歌设置,并启用"不太安全"的应用程序.它会工作.它为我工作.
小智 7
正如其他人已经建议的那样,您可以启用"不太安全"的应用程序,或者只需切换ssl到tls:
$mailer->Host = 'tls://smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->Username = "xxx@gmail.com";
$mailer->Password = "***";
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;
Run Code Online (Sandbox Code Playgroud)
使用时tls,无需为安全性较低的应用程序授予访问权限,只需确保已启用IMAP.
小智 2
我面临同样的问题,并且认为我确实知道为什么会发生这种情况。
我使用的 gmail 帐户通常是在印度使用的,而我使用的网络服务器位于荷兰。
Google 通知有人从异常位置尝试登录,并要求通过网络浏览器从该位置登录。
此外,我必须通过https://security.google.com/settings/security/activity接受对 gmail 帐户的可疑访问
但最终我的问题还没有解决,因为我必须从荷兰的某个位置登录gmail。
我希望这对你有一点帮助!(抱歉,我不会阅读此电子邮件地址上的电子邮件回复)