PHPMailer中的SMTP connect()失败错误

时间:2013-10-10 06:01:16

标签: php email smtp

我是PHP新手,我想使用PHP发送邮件。我有一个联系我们表格,我会接受与我联系的人的电子邮件,因此邮件将发送给我。我正在使用https://github.com/PHPMailer/PHPMailer/tree/master中的PHPMailer库,以下是我正在使用的代码段。

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();  

$mail->SMTPSecure = 'tls';

$mail->Host     = "resolver1.opendns.com"; // this SMTP server of my machine
//$mail->Host     = "208.67.222.222";//ip ; which one to use the resolver1.opendns.com or 208.67.222.222 ???

$mail->From     = "xyz@gamil.com;//email id of the person 

$mail->AddAddress("datta.dhonde@coreathena.com");//my email id

$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.';
}
?> 

我收到错误消息“未发送消息.Mailer错误:SMTP连接()失败。” 我没有得到什么问题..? $ mail-&gt; Host =“”;请评论这是什么意思?

4 个答案:

答案 0 :(得分:23)

添加$mail->SMTPDebug = 1;并尝试调试问题。

答案 1 :(得分:4)

正如@joydesigner所示,要通过SMTP连接,您需要通过hostname, username and password,然后才能连接并发送电子邮件。

$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // tls or ssl connection as req

在此我看到您已经过了host个信息,请添加username & password并尝试一次。

同时检查您的服务器是否已打开TLS/SSL PORT

检查:

telnet resolver1.opendns.com 25

答案 2 :(得分:1)

可能是您的配置问题。

phpmailer配置示例如下:

<?php
 require 'class.phpmailer.php';

 $mail = new PHPMailer;

 $mail->isSMTP();                                      // Set mailer to use SMTP
 $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
 $mail->SMTPAuth = true;                               // Enable SMTP authentication
 $mail->Username = 'jswan';                            // SMTP username
 $mail->Password = 'secret';                           // SMTP password
 $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

 $mail->From = 'from@example.com';
 $mail->FromName = 'Mailer';
 $mail->addAddress('josh@example.net', 'Josh Adams');  // Add a recipient
 $mail->addAddress('ellen@example.com');               // 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;
  exit;
 }

 echo 'Message has been sent';

这里的$ mail-&gt;主机是smtp邮件服务器。通常以smtp开头。

答案 3 :(得分:0)

您应该检查resolver1.opendns.com的tcp端口25,它会阻止或不启动stmpd,例如sendmail或某些MTA。

试 telnet resolver1.opendns.com 25

你会发现tcp端口25没有打开。