我的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
$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.';
}
?>当我运行它时,我会得到这样的错误:
2015-05-18 13:46:19 SERVER -> CLIENT: 2015-05-18 13:46:19 SMTP NOTICE: EOF caught while checking if connected 2015-05-18 13:46:19 SMTP Error: Could not authenticate. 2015-05-18 13:46:19 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting发布于 2015-05-18 14:19:21
注意事项:编辑
您必须在上面的代码中包括下面一行,然后再检查一遍。
$mail->SMTPSecure = 'tls';始终通过传递PHPMailer参数初始化true,因为它帮助您捕获异常。
$mail = new PHPMailer(true);然后在try块中放置您发送电子邮件的代码,然后您就可以捕获这样的异常
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);
}https://stackoverflow.com/questions/30305038
复制相似问题