【发布时间】:2016-02-21 16:40:45
【问题描述】:
我想使用 SMTP 服务器 (gmail) 从 localhost XAMPP 使用 phpmailer 发送邮件。但我不断收到此错误:
无法发送邮件。邮件程序错误:SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
我尝试了很多关于在 php.ini 文件中取消注释 openSSL 的解决方案,更改端口 465("ssl") 和 587("tls"),但它不起作用。
我的代码:
<?php
date_default_timezone_set('Etc/UTC');
'PHPMailerAutoLoad.php';
class.phpmailer.php if not already loaded
$port =465;
$securetype = 'ssl';
$from = 'myemail@gmail.com';
$name = 'User';
$toemail= "mymail@gmail.com";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->isSMTPDebug = 1;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myemail@gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = $securetype;
$mail->Port = $port;
$mail->From = $from;
$mail->FromName = $name;
$mail->addAddress($toemail);
$mail->isHTML(true);
$mail->Subject = 'Test Mail Subject!';
$mail->Body = 'This is SMTP Email Test';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
【问题讨论】:
-
您使用的是旧的 gmail 示例,表明您尚未阅读或完成链接到的文档告诉您的内容。
标签: email smtp xampp phpmailer