Search code examples
phpsmtpgmailphpmailer

Can't access my gmail smtp client with an app password in php


So, I use phpMailer to send mails throught gmail's smtp, here is my code

use PHPMailer\PHPMailer\PHPMailer;
require 'vendor\autoload.php';
define('GMailUSER', '[email protected]');
define('GMailPWD', '****************');
function smtpMailer($to, $from, $from_name, $subject, $body) {
  $mail = new PHPMailer();
  $mail->IsSMTP();
  $mail->SMTPDebug = 1;
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = 'ssl';
  $mail->SMTPOptions = array(
    'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
    )
  );
  $mail->Host = 'smtp.gmail.com';
  $mail->Port = 465;
  $mail->Username = GMailUser;
  $mail->Password = GMailPWD;
  $mail->SetFrom($from, $from_name);
  $mail->Subject = $subject;
  $mail->Body = $body;
  $mail->AddAddress($to);
  if(!$mail->Send()) {
    return 'Mail error: '.$mail->ErrorInfo;
  } else {
    return true;
  }
}

$result = smtpmailer('[email protected]', '[email protected]', 'Giga Battleboard', 'Message', 'Subject');
  if (true !== $result)
{
  echo $result;
}

I created an app password on the google account I use, after having activated the two-factor verification. But I still get the same error:

2023-01-13 14:21:05 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials s23-20020a1cf217000000b003d1e3b1624dsm29449744wmc.2 - gsmtp SMTP Error: Could not authenticate.

Unfortunately, the link given by the error did not bring me anything conclusive. How do I get out of this?


Solution

  • The apps password needs to be use in your code in place of your actual google password. If you are still seeing Username and Password not accepted. then you have not used the apps password in your code.

    $mail->Username = GMailUser;
    $mail->Password = AppsPassWord;
    

    If that doesn't work let me know I should have a PHP sample floating around.

    Your code runs fine with an apps password

    I just ran your code. The only thing i changed was fixing the constant and setting the from to that of the constant rather then hard coding your email address. It runs fine

    <?php
    
    // Run composer require phpmailer/phpmailer
    
    use PHPMailer\PHPMailer\PHPMailer;
    require 'vendor\autoload.php';
    
    const GMailUSEREmail = 'MyEmailAddress';
    const GoogleAppsPassword = 'MyAppsPassword';
    
    function smtpMailer($to, $from, $from_name, $subject, $body): bool|string
    {
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPDebug = 1;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->Host = 'smtp.gmail.com';
        $mail->Port = 465;
        $mail->Username = GMailUSEREmail;
        $mail->Password = GoogleAppsPassword;
        $mail->SetFrom($from, $from_name);
        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->AddAddress($to);
        if(!$mail->Send()) {
            return 'Mail error: '.$mail->ErrorInfo;
        } else {
            return true;
        }
    }
    
    $result = smtpmailer(GMailUSEREmail, GMailUSEREmail, 'Giga Battleboard', 'Message', 'Subject');
    if (true !== $result)
    {
        echo $result;
    }
    

    The email sent

    enter image description here