phpmailer 无法连接到 SMTP 服务器 [英] phpmailer does not connect to SMTP servers

查看:98
本文介绍了phpmailer 无法连接到 SMTP 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 phpmailer 并尝试使用 live.com 和 gmail.com,但它始终无法连接到 SMTP 服务器.这是完整的代码(我已经使用 smtp.live.com 尝试过 live.com,但我遇到了同样的问题无法发送邮件.邮件程序错误:SMTP 连接()失败.")

i've been trying to use phpmailer and tried using live.com and gmail.com but it always can't connect to SMTP server. here's the full code (i've tried live.com using smtp.live.com but i get the same problem "Message could not be sent.Mailer Error: SMTP connect() failed.")

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$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 = 'my emil address';                 // SMTP username
$mail->Password = 'my password';                           // SMTP password
$mail->SMTPSecure = 'TLS';                            // Enable encryption, 'ssl' also accepted

$mail->From = 'the same email address';
$mail->FromName = 'Mailer';
//$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('another email address');               // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

推荐答案

我怀疑是因为你没有设置端口,SMTPSecure 应该是小写的.改变这个:

I suspect it's because you've not set the port, and SMTPSecure should be lower case. Change this:

$mail->SMTPSecure = 'tls';
$mail->Port = 587;

除此之外,请检查您的 ISP/防火墙是否允许您发送出站邮件,以及您的 DNS 是否正常工作.

Beyond that, check that you're allowed to send outbound mail by your ISP/firewall and that your DNS is working.

这篇关于phpmailer 无法连接到 SMTP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆