PHPMailer不支持Gmail SMTP
问题描述:
以下代码适用于我的xampp本地服务器,但不会在远程主机上发送电子邮件。我得到这个错误:PHPMailer不支持Gmail SMTP
Message could not be sent.Mailer Error: SMTP connect() failed
require_once('header.php');
require_once('PHPMailer/PHPMailerAutoload.php');
function sendMail($address, $message){
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'mypass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->setFrom('[email protected]', 'ID Test'); // Add a recipient
$mail->addAddress($address); // Name is optional
//$mail->addReplyTo('[email protected]', 'Information');
//$mail->addCC('[email protected]');
//$mail->addBCC('[email protected]');
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Twitter search';
$mail->Body = $message;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
}
答
我有麻烦与PHPMailer的过去,当使用Gmail的服务器设置为TLS认证协议和端口号587,我不记得曾经的组合为我工作。不过,我从来没有遇到过使用SSL/465的问题。
取而代之的是:
// Your Current Settings
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
试试这个:
// Updated Settings
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
的更多信息:
http://php.net/manual/en/function.error-reporting.php --- https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting –
还有一个PhpMailer的调试标志,读取文档,搜索类似的问题。 – Progrock