phpmailer不连接到SMTP服务器

时间:2014-06-28 06:39:46

标签: smtp phpmailer

我一直在尝试使用phpmailer并尝试使用live.com和gmail.com但它始终无法连接到SMTP服务器。这里是完整的代码(我已经尝试使用smtp.live.com的live.com,但我得到了同样的问题“消息无法发送.Mailer错误:SMTP连接()失败。”)

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';
}

2 个答案:

答案 0 :(得分:1)

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

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

除此之外,请检查您是否允许您的ISP /防火墙发送出站邮件,并且您的DNS正在运行。

答案 1 :(得分:0)

试试这个:

$mail->SMTPSecure = 'tls'; 
$mail->Host = 'tls://smtp.gmail.com';
$mail->Port = 587; //You have to define the Port
$mail->SMTPDebug  = 3;

删除它:

$mail->Host = 'smtp.gmail.com'; 
$mail->SMTPSecure = 'TLS';