Search code examples
wordpressphpmailer

Gmail SMTP blocking real server not localhost


I have configured phpmailer for my wordpress site following way:

<?php
add_action( 'phpmailer_init', 'setup_smtp');
function setup_smtp( $phpmailer ) {
    if ( ! is_object( $phpmailer ) ) {
        $phpmailer = (object) $phpmailer;
    }
    
    $phpmailer->Mailer     = 'smtp';
    $phpmailer->Host       = 'smtp.gmail.com';
    $phpmailer->SMTPAuth   = true;
    $phpmailer->Port       = '465';
    $phpmailer->Username   = 'duet383@gmail.com';
    $phpmailer->Password   = 'xxxxxxxx';
    $phpmailer->SMTPSecure = 'ssl';
    $phpmailer->From       = 'duet383@gmail.com';
    $phpmailer->FromName   = 'Asian Restaurant';
    $phpmailer->SMTPDebug   = 2;

}

It is sending email when it is on localhost but when on real server it is giving following error:

2021-05-05 17:28:55 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 26sm5525488qtd.73 - gsmtp
2021-05-05 17:28:55 CLIENT -> SERVER: EHLO miraj94.000webhostapp.com
2021-05-05 17:28:55 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2a02:4780:bad:3:fced:1ff:fe03:76]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-05-05 17:28:55 CLIENT -> SERVER: AUTH LOGIN
2021-05-05 17:28:55 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-05-05 17:28:55 CLIENT -> SERVER: [credentials hidden]
2021-05-05 17:28:55 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-05-05 17:28:55 CLIENT -> SERVER: [credentials hidden]
2021-05-05 17:28:56 SERVER -> CLIENT: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu534-5.7.14 mBCp0KPnA6lLVDXtO3PD4WNqyB_meMsjJFlkSNZ9vC63f-RVlCMCuQTTtGyTG_O-Udkkr534-5.7.14 ll7IKMM31GmIn6raNGiiVq6cQEREBB9fClbMYCyhqBphXb93cJjmcAqwJXJw0QxJ>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 26sm5525488qtd.73 - gsmtp
2021-05-05 17:28:56 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu534-5.7.14 mBCp0KPnA6lLVDXtO3PD4WNqyB_meMsjJFlkSNZ9vC63f-RVlCMCuQTTtGyTG_O-Udkkr534-5.7.14 ll7IKMM31GmIn6raNGiiVq6cQEREBB9fClbMYCyhqBphXb93cJjmcAqwJXJw0QxJ>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 26sm5525488qtd.73 - gsmtp
SMTP Error: Could not authenticate.
2021-05-05 17:28:56 CLIENT -> SERVER: QUIT
2021-05-05 17:28:56 SERVER -> CLIENT: 221 2.0.0 closing connection 26sm5525488qtd.73 - gsmtp
SMTP Error: Could not authenticate.

I am using free web hosting and domain on 000webhost.com and my site address is https://miraj94.000webhostapp.com


Solution

  • After long study I found a solution.

    1. Login to my Gmail account
    2. Go to https://accounts.google.com/b/0/DisplayUnlockCaptcha
    3. Click on continue button
      [Less secure app access was already enabled]

    Additionally this stackoverflow answer maybe helpful.