简体   繁体   中英

PHPMailer SMTP connection failed error

In my code I am just trying to send an email to a recipient using the PhPmailer tool but every time I do i get errors. I've watched numerous amounts of video tutorials on this and looked at tons of websites but no fix. I've tried changing my port from "587 to 465", change the "SMTPDebug = 2" to see clear error message, I even tried troubleshooting from the github page but I am still having no luck. Here is my code

<?php
require 'PHPMailerAutoload.php';
require 'credential.php';

$mail = new PHPMailer;

$mail->SMTPDebug = 4;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.gmail.com;';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = EMAIL;                 // SMTP username
$mail->Password = PASS;                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom(EMAIL, 'Testing this');
$mail->addAddress('testemail123@outlook.com');     // Add a recipient              // Name is optional
$mail->addReplyTo(EMAIL);
$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';
}
?>

Errors Messages

2018-02-23 07:16:07 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ()
2018-02-23 07:16:07 Connection: opened
2018-02-23 07:16:07 SMTP -> get_lines(): $data is ""
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "220 smtp.gmail.com ESMTP s125sm758477vkb.40 - gsmtp"
2018-02-23 07:16:07 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP s125sm758477vkb.40 - gsmtp
2018-02-23 07:16:07 CLIENT -> SERVER: EHLO localhost
2018-02-23 07:16:07 SMTP -> get_lines(): $data is ""
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-smtp.gmail.com at your service, [x.x.x.x]"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-SIZE 35882577"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-8BITMIME"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-STARTTLS"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-ENHANCEDSTATUSCODES"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-PIPELINING"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-CHUNKING"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250 SMTPUTF8"
2018-02-23 07:16:07 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-02-23 07:16:07 CLIENT -> SERVER: STARTTLS
2018-02-23 07:16:07 SMTP -> get_lines(): $data is ""
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "220 2.0.0 Ready to start TLS"
2018-02-23 07:16:07 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2018-02-23 07:16:07 Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed [C:\wamp64\www\mailer\phpmailer\class.smtp.php line 375]
SMTP Error: Could not connect to SMTP host.
2018-02-23 07:16:07 CLIENT -> SERVER: QUIT
2018-02-23 07:16:07 SMTP -> get_lines(): $data is ""
2018-02-23 07:16:07 
2018-02-23 07:16:07 
2018-02-23 07:16:07 
2018-02-23 07:16:07 
2018-02-23 07:16:07 
2018-02-23 07:16:07 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

You are getting a DNS error that says it can't find the host name. So look at your host name property, and see it contains smtp1.gmail.com when it should be smtp.gmail.com .

1.use SSL

2.add this in your php file:

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

3.enable from myaccount.gmail.com -> Sign-in & security -> Apps with account access

  "Allow less secure apps: ON" 

done.

If anyone stuck with godaddy email sending this and its throwing Connection failed. Error #2: stream_socket_enable_crypto(): Connection failed. Error #2: stream_socket_enable_crypto(): Error then use this snippet to send mail

   $mail->isSMTP();
    $mail->SMTPDebug = true;
    $mail->SMTPOptions = array(
      'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
      )
    );
    $mail->Host = 'localhost'; 
    $mail->Username = "email@yourdomian.com";
    $mail->Password = "ypurpass";
    $mail->Port = 25;
    $mail->CharSet = 'utf-8';
    $mail->setFrom('faisal.aqurz@gmail.com', (empty($name) ? 'Contact form' : $name));
    $mail->addAddress($_POST['email']);
    $mail->addReplyTo('faisal.aqurz@gmail.com', 'Information');
    $mail->addCC('faisal.aqurz@gmail.com');
    //        // Attachments
    $mail->addAttachment($attachmentToBesent['file'], 'File.pdf');         // Add attachments
    $mail->Subject = 'New message from website visitor!';
    $mail->Body = "Contact form submission\n\n" . $message;
    $mail->ssl = false;
    $mail->isHTML(true);
    $mail->authentication = false;
    $mail->AltBody = 'Sorry your browser does not support this email';
    if (!$mail->send()) {
      $msg .= "Mailer Error: " . $mail->ErrorInfo;
    } else {
      $msg .= "Message sent!";
    }
    $mail->send();
  

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