简体   繁体   中英

SMTP Error: Could not connect to SMTP host using PHPMailer

we are using SMTP advanced authentication to send a test mail using PHPMailer. We are using 1and1.com server with SMTP and SSL for E-mail Exchange. We need to run this php page from a third party server. we have taken a example from the downloaded PHPMailer package. we have tried with "test_pop_before_smtp_advanced" example and " SMTP advanced test with authentication" example. In both cases we are getting same Error.

SMTP Error: Could not connect to SMTP host.

Here is the php file we have written for sending Mail.

<html>
<head>
<title>PHPMailer - SMTP advanced test with authentication</title>
</head>
<body>

<?php
echo "hai";
include('class.phpmailer.php');
include('class.smtp.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch


$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
echo "hai1";
$mail->IsSMTP(); // telling the class to use SMTP

try {

  $mail->Host       = "smtp.1and1.com"; // SMTP server
  $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "smtp.1and1.com"; // sets the SMTP server
  $mail->Port       = 587;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "xxx@abc.com"; // SMTP account username
  $mail->Password   = "xxxxx";        // SMTP account password
  $mail->AddReplyTo('mno@abc.com', 'First Last');
  $mail->AddAddress('mno@abc.com', 'John Doe');
  $mail->SetFrom('xxx@abc.com', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->Body = 'hello';

 if(!$mail->Send()) {
   echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
   echo "Message sent!";
}
} catch (phpmailerException $e) {
echo "error";
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo "err";
  echo $e->getMessage(); //Boring error messages from anything else!
}
?>

</body>
</html>

Can anyone help us in this regard, please suggest any other easy approach for the same purpose. Thank you...

I had the same problem, and was walked through a bunch of different port, domain, and security configuration attempts. Try to use these settings, I found them on another help site and they worked for me.

$mail->IsSMTP();                                    // Set mailer to use SMTP
$mail->Host = '74.208.5.2';             // Specify main and backup SMTP server (server name)
$mail->Port = 25;                // TCP port to connect to 587 or 465, 425, 25 

$mail->Username = 'xxxxx@xxx.com';  // SMTP username
$mail->Password = 'xxxxxx';        // SMTP password

$mail->SMTPAuth = true;       // Enable SMTP authentication
$mail->SMTPSecure = 'tls';     // Enable TLS encryption, `ssl` also accepted

I had to set SMTPSecutre to tls or remove this line completely, use smtp.1and1.net's ip for the host (you can verify with a ping or whois), and set port to 25. Even though 1and1 suggested other ports more often. Hopefully this saves someone some time.

PHP suddenly changed in version 5.6 to be much more strict about SSL connections. This causes many web contact forms to break, often without any error message being displayed. If you want to continue using a self-signed certificate, or allowing a non-SSL (insecure) connection, a quick workaround for PHPMailer is to use:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

if you are using an SSL connection - try changing your port to default SSL port 465

If you are not using SSL, try removing these lines:

$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";

and change the port to default port 25.

As you are getting a could not connect, take a moment to check your login details are correct, even the best of us can fall foul of a miss-typed password.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM