0

I am getting this error. I need to implement mail notification feature in my project. I am using PHP mailer. And host is gmail only. Here I am unable to connect for host. I don't no what is the issue.. please let me know...

    <?php 
    // Import PHPMailer classes into the global namespace
    // These must be at the top of your script, not inside a function
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    //Load composer's autoloader
    require 'vendor/autoload.php';

    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
        //Server settings
        $mail->SMTPDebug = 2;                                 // Enable verbose debug output
        $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 = 'XXXXXX@gmail.com';                 // SMTP username
        $mail->Password = 'XXXXX';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                    // TCP port to connect to

        //Recipients
        $mail->setFrom('XXXXXXX@gmail.com', 'XXX');
        $mail->addAddress('XXXXXXX1@gmail.com');     // 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');

        //Attachments
       // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
      //  $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

        //Content
        //$mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'This is the test email';
        $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';

        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }
    ?>

Error:

2017-12-19 13:12:44 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP s81sm32492847pfg.60 - gsmtp
2017-12-19 13:12:44 CLIENT -> SERVER: EHLO localhost
2017-12-19 13:12:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [106.201.62.204]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-12-19 13:12:45 CLIENT -> SERVER: STARTTLS
2017-12-19 13:12:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2017-12-19 13:12:45 CLIENT -> SERVER: QUIT
2017-12-19 13:12:45 
2017-12-19 13:12:45 
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.
Bhaumik Pandhi
  • 2,655
  • 2
  • 21
  • 38
Avinash T
  • 55
  • 3
  • 9
  • 1
    On your Gmail security tab, set "Allow less secure applications" to true. – Dmitry Malys Dec 19 '17 at 13:26
  • @DmitryMalys — How will that help when it can't connect to the host? GMail doesn't know if the client is less secure or not if there is no connection in the first place. – Quentin Dec 19 '17 at 13:27
  • Wild speculation: Your web hosting service forbids outgoing SMTP connections. Talk to them about your problem. – Quentin Dec 19 '17 at 13:28
  • @DmitryMalys in security i am not able to find "allow less secure app" can u pls give me details steps... – Avinash T Dec 19 '17 at 13:32
  • @Quentin i have not hosted this application.. i am doing it on my local computer. Is there any solution for this issue? – Avinash T Dec 19 '17 at 13:33
  • Try here https://support.google.com/accounts/answer/6010255?hl=en but as Quentin told, I'm not sure if it will help. – Dmitry Malys Dec 19 '17 at 13:34
  • @DmitryMalys Its not working :( – Avinash T Dec 19 '17 at 13:39
  • I'm afraid Quentin is right in some way, I just tried your code and I received a letter from google, which says about blocking less secure app – Dmitry Malys Dec 19 '17 at 13:58
  • Have you tried this? https://stackoverflow.com/questions/18496650/smtp-connect-failed-message-was-not-sent-mailer-error-smtp-connect-failed – Dmitry Malys Dec 19 '17 at 14:03
  • @DmitryMalys yes i used that same code... Getting smtp connection error – Avinash T Dec 20 '17 at 05:53

0 Answers0