SMTP connect() failed error in PHPMailer [closed]

2020-02-06 02:57发布

I am completely new to PHP and I want to send a mail using PHP. I have a Contact Us form whcih will accept email is of the person contacting me and therefore the mail will be sent to me. I am using PHPMailer library from https://github.com/PHPMailer/PHPMailer/tree/master and following is the code snippet I am using.

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

I am getting the error " Message was not sent.Mailer error: SMTP connect() failed." I am not getting what is the problem..? $mail->Host = ""; please comment on what this stands for??

标签: php email smtp
4条回答
Evening l夕情丶
2楼-- · 2020-02-06 03:16

Add $mail->SMTPDebug = 1; and try to debug the problem.

查看更多
萌系小妹纸
3楼-- · 2020-02-06 03:20

As very well exampled by @joydesigner, To connect via SMTP, you will need to pass hostname, username and password and then it should connect and send email.

$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

Here I see you have passed only, host information, pls add username & password as well and try once.

Also check that TLS/SSL PORT is open for your server:

check with:

telnet resolver1.opendns.com 25
查看更多
你好瞎i
4楼-- · 2020-02-06 03:28

Maybe it is your configuration problem.

example of phpmailer configuration is like this:

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

Here the $mail->Host is the smtp mail server. Normally started with smtp.

查看更多
霸刀☆藐视天下
5楼-- · 2020-02-06 03:30

You should check the tcp port 25 of the resolver1.opendns.com , it seens being block or not starting up the stmpd such as sendmail or some MTA.

try telnet resolver1.opendns.com 25

and you will find tcp port 25 is not opened.

查看更多
登录 后发表回答