我试图使用phpmailer发送邮件,但收到错误"SMTP connect()failed"。我已经允许在gmail上使用不太安全的应用程序。所有smtp设置都正确吗?请指引我哪里错了。
<?php
require_once 'PHPMailer-master/PHPMailerAutoload.php';
require 'PHPMailer-master/class.phpmailer.php';
require 'PHPMailer-master/class.smtp.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->host = 'smtp.gmail.com';
$mail->username = 'mymail@gmail.com';
$mail->password = 'mypassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = true;
$mail->isHTML();
$mail->Subject = 'form data';
$mail->Body = 'this is the body of message';
$mail->FromName = 'The Form';
$mail->AddAddress('junaidshekh21@gmail.com','Junaid Shaikh');
if($mail->send())
{
echo "sent successfully";
die();
}
else
{
echo "could not send";
}
?>
在phpmail
中使用ErrorInfo
查看的错误
在发送电子邮件之前添加此
$mail->SMTPDebug = 2; //enables SMTP debug information (for testing)
和的变化
echo "could not send";
至
echo "Mailer Error: " . $mail->ErrorInfo;
注意:
- 阅读phpmail疑难解答,特别是有关关于"SMTP错误:无法连接到SMTP主机。"
- 使用phpmailer-github-reo中的官方gmail示例
- 你可能不得不允许不太安全的应用程序访问你的gmail帐户
- 你的ip可能会被谷歌屏蔽
更新:
来自官方phpmailer
gmail示例:
$mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
您的PHPmailer设置还可以,我认为这是不太安全的应用程序访问问题。你必须让你的gmail变得不那么安全才能访问。已访问此urlhttps://www.google.com/settings/security/lesssecureapps以SMTP形式访问您的gmail。