Php SMTP错误:无法进行身份验证
我对mail()函数有一些问题,所以当我尝试使用PHPmailer发送邮件时,下面的代码是我从一个教程中复制的,它给了我错误信息Php SMTP错误:无法进行身份验证,php,email,phpmailer,Php,Email,Phpmailer,我对mail()函数有一些问题,所以当我尝试使用PHPmailer发送邮件时,下面的代码是我从一个教程中复制的,它给了我错误信息 <?php include("PHPMailer-master/class.phpmailer.php"); include('PHPMailer-master/class.smtp.php'); $mail = new PHPMailer(); $mail->IsSMTP(); // telling the class to use SMTP $mai
<?php
include("PHPMailer-master/class.phpmailer.php");
include('PHPMailer-master/class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Username = "nati323@gmail.com";
$mail->Password = "SOMEPASS";
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->SMTPDebug = 2;
$mail->From = "from@example.com";
$mail->AddAddress("nati323@gmail.com");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
注意:[编辑] 您必须在上述代码中包含以下行,然后再次检查
$mail->SMTPSecure = 'tls';
始终通过传递true
参数初始化PHPMailer
,因为它有助于捕获异常
$mail = new PHPMailer(true);
然后在中尝试
阻止输入您发送电子邮件的代码
然后,您可以捕获这样的异常
catch (phpmailerException $e) {
echo $e->errorMessage(); //PHPMailer error messages
} catch (Exception $e) {
echo $e->getMessage(); //other error messages
}
从这里获取最新的PHPMailer示例
编辑:
更改class.smtp.php文件,可能在238行左右
public function connect($host, $port = null, $timeout = 30, $options = array()) {
if (count($options) == 0) {
$options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);
}
我敢打赌你的
from
不会是from@example.com
。它可能必须是nati323@gmail.com
。Gmail不允许你从其他地址发送邮件。也许你是对的,但问题似乎出在SMTP连接上。检查你的电子邮件帐户。如果你收到一封由谷歌发送的电子邮件,可以通过不太安全的应用程序进行访问…启用该功能,然后重试..Gmail需要tls连接,请尝试添加$mail->SMTPSecure='tls'看看是否有帮助。我没有收到这样的邮件,我还尝试添加tls,它给了我一个php错误:警告:stream\u socket\u enable\u crypto():SSL操作失败,代码为1。OpenSSL错误消息:错误:14090086:SSL例程:SSL3\U GET\U SERVER\U证书:证书验证在C:\xampp\htdocs\site\diamonds\PHPMailer master\class.smtp.php中失败,最好指向。它们非常相似,但都是最新的。嘿,我照你说的做了,就像我之前在尝试将SMTPsecure设置为tls时所说的,我得到了以下php错误:警告:stream\u socket\u enable\u crypto():SSL操作失败,代码为1。OpenSSL错误消息:错误:14090086:SSL例程:SSL3_GET_SERVER_证书:证书验证在C:\xampp\htdocs\site\diamonds\PHPMailer master\class.smtp.phphphy中失败。我刚刚检查了它…似乎您必须使用php版本5.6。PHP5.6允许ssl证书验证。检查我的编辑嘿,非常感谢,现在工作很好,你不知道你帮了我多少忙,因为今天早上(8小时前)我一直试图通过php发送电子邮件,现在我可以做到这一点,谢谢你
public function connect($host, $port = null, $timeout = 30, $options = array()) {
if (count($options) == 0) {
$options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);
}