Search code examples
phpemailgmailphpmailer

Can't Send Email through PHP on localhost using Gmail account


On 30 May 2022 Google changed "Less secure apps" settings and it's no longer available.

Less secure apps & your Google Account

To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.

Due to which I'm not able to send Email on localhost using PHP.

Is there any other way to do this ?


Solution

  • I am going to assume you are connecting to Gmail via the smtp server.

    If you are currently using the actual password for the gmail account you are connecting to. The the best thing to do would be to try creating an apps password and using that.

    Sign in with App Passwords

    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = '[email protected]';                     //SMTP username
    $mail->Password   = 'APPsPassword';                               //New apps password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;                      
    

    Another option would be to switch to Xoauth or switching to the Gmail api again using Oauth2.

    $mail->oauthUserEmail = "[Redacted]@gmail.com";
    $mail->oauthClientId = "[Redacted]";
    $mail->oauthClientSecret = "[Redacted]";
    $mail->oauthRefreshToken = "[Redacted]";