Authentication error while sending emails with Gmail SMTP server in CakePHP 4

Hi all,

In my local (!) CakePHP 4 installation, I want to send emails via the Gmail SMTP server. Unfortunately, I constaltly have the same error when I try to send an email:

SMTP Error: 530 5.7.0 https://support.google.com/mail/?p=WantAuthError h9-20020a05600c314900b003d99469ece1sm6804459wmo.24 - gsmtp

After Googling around, I found out that this is an authentication problem. See https://bobcares.com/blog/smtp-error-530/

Back in time (in my CakePHP2 installation) I used the ‘Less secure apps’ from Google and that always worked great. But since May 2022, it is no longer supported by Google. Nowadays, we should use ‘App passwords’ and that was exactly what I did:

As you can see in this screenshot (sorry, it’s in Dutch), I selected the option ‘Other (custom name)’. Then, I copied the given password into notepad.
(Little note: the CakePHP 4 cookbook is outdated, it still tells us to use ‘Less secure apps’, see Mailer - 4.x)

Then, I created a new Mailer in ArticlesController.php:

$mailer = new Mailer('sabasco');
$mailer->setEmailFormat('html')
    ->setSubject(__('Comment on article'))
    ->setReplyTo($comment->email, $comment->name)
    ->setViewVars([
        'id' => $comment->id,
        'name' => $comment->name,
        'email' => $comment->email,
        'content' => nl2br($comment->content),
        'articleId' => $comment->articleId,
        'articleName' => $article->name
    ])
    ->viewBuilder()
        ->setLayout('default')
        ->setTemplate('comment');
$mailer->deliver();

Then, I configured my app.php like this:

'EmailTransport' => [
    'gmail' => [
        'host' => 'smtp.gmail.com',
        'port' => 587,
        'username' => 'sabasco.development@gmail.com',
        'password' => '16DigitsPasswordFromNotepad',
        'className' => 'Smtp',
        'tls' => true
    ]
],
'Email' => [
    'sabasco' => [
        'transport' => 'gmail',
        'to' => ['hello@myemailaddress.be' => 'Sabasco'],
        'from' => ['hello@myemailaddress.be' => 'Sabasco']
    ]
],

So this setup doesn’t work and I totally don’t understand why. I also tried to change the EmailTransport in many ways, like this:

'EmailTransport' => [
    'gmail' => [
        'host' => 'smtp.gmail.com',
        'port' => 587,
        'username' => 'sabasco.development@gmail.com',
        'password' => '16DigitsPasswordFromNotepad',
        'client' => null,
        'className' => 'Smtp',
        'tls' => true,
        'context' => [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            ]
        ]
    ]
],

or like this:

'EmailTransport' => [
	'gmail' => [
		'host' => 'ssl://smtp.gmail.com',
		'port' => 587,
		'username' => 'sabasco.development@gmail.com',
		'password' => '16DigitsPasswordFromNotepad',
		'client' => null,
		'className' => 'Smtp',
		'context' => [
			'ssl' => [
				'verify_peer' => false,
				'verify_peer_name' => false,
				'allow_self_signed' => true
			]
		]
	],
],

I also tried to change the port to 25 or 465, but I always got the same SMTP 530-error :frowning:

Some other developers had issues with an antivirus program. I’m using Bitdefender, but I don’t find any options where I can disable some mail related options. So I don’t think it has something to do with my problem.

I’ve been struggeling around many hours with this issue and I reached the point where I have no other options to call for your help :slight_smile:
Does anybody have a clue what I’m doing wrong?

Many many thanks for your support!
I hope my question and your answer can help other developers too <3

I have no problems with Google SMTP with the following settings:

'Email' => [
    'gmail' => [
        'transport' => 'gmail',
        'from' => ['myaddress@gmail.com' => 'Lokal debug email']
    ]
],
'EmailTransport' => [
    'gmail' => [
        'host' => 'smtp.gmail.com',
        'port' => 587,
        'username' => 'myaddress@gmail.com',
        'password' => 'my16charapppassword',
        'className' => \Cake\Mailer\Transport\SmtpTransport::class,
        'tls' => true
    ]
]

then created a simple Mailer class via bin/cake bake mailer Debug
and added this method

class DebugMailer extends Mailer {
    /**
     * Usage: $this->getMailer('Debug')->send('test');
     *
     * @return self
     */
    public function test(): self
    {
        $this->setTransport('gmail')
            ->setTo('myprivate@private.com)
            ->setSubject('Debug Email')
            ->setEmailFormat('html')
            ->setViewVars([
                // view vars which are present in your html email template
            ])
            ->viewBuilder()
            ->setTemplate('default'); // Use the same layout file for all

        return $this;
    }
}

and finally I added this to a random controller which triggers the email

class RandomController extends AppController
{
    use \Cake\Mailer\MailerAwareTrait;

    public function index(): void
    {
        $this->getMailer('Debug')->send('test');

        // rest of index action code
    }
}

Hi Kevin, thanks for your reply and for your efforts! One additional question: did you set up an app too or did you only use your gmail credentials?

I did create a app password with the type Other just like you described at the start.

and that generated app password is used in the 'password' => 'my16charapppassword', inside the cakephp config.

Regarding any anti-virus problem or IP connection problem: I don’t think that its related to something like that because you get a SMTP Error: 530 5.7.0 error which means your app can at least establish a connection and get an answer.

All I can say is that I have a 2FA Code active on my Google Account, which therefore doesn’t even allow me to enable “Less secure app access” under https://myaccount.google.com/u/1/lesssecureapps

All I have is that app password.

Once again: thanks for your reply @KevinPfeifer!

I’ve been searching around a little bit deeper. In ‘SmtpTransport.php’ for example, I’ve done 2 debugs in the ‘_smtpSend()’ function.

This is my first debug:

debug($checkCode);

This results in:

CORE\src\Mailer\Transport\SmtpTransport.php (line 557)
‘220’

It means that at this point, the SMTP server is ready to continue successfully. Your were right @KevinPfeifer: the connection with the SMTP server was successfully made. So far so good!

This is my second debug in the ‘_smtpSend()’ function:

debug($this->_socket());

This results in:

CORE\src\Mailer\Transport\SmtpTransport.php (line 562)

object(Cake\Network\Socket) id:0 {
protected _defaultConfig => [
'persistent' => false,
'host' => 'localhost',
'protocol' => 'tcp',
'port' => (int) 80,
'timeout' => (int) 30,
]
protected connection => (resource) Resource id #429
protected connected => true
protected lastError => [ ]
protected encrypted => false
protected _encryptMethods => [
'sslv23_client' => (int) 57,
'tls_client' => (int) 121,
'tlsv10_client' => (int) 9,
'tlsv11_client' => (int) 17,
'tlsv12_client' => (int) 33,
'sslv23_server' => (int) 120,
'tls_server' => (int) 120,
'tlsv10_server' => (int) 8,
'tlsv11_server' => (int) 16,
'tlsv12_server' => (int) 32,
]
protected _connectionErrors => [ ]
protected _config => [
'persistent' => false,
'host' => 'smtp.gmail.com',
'protocol' => 'tcp',
'port' => (int) 587,
'timeout' => (int) 30,
'username' => 'sabasco.development@gmail.com',
'password' => 'mypassword',
'client' => null,
'tls' => true,
'keepAlive' => false,
'className' => 'DebugKit.DebugKit',
'url' => null,
'context' => [
'ssl' => [ ],
],
]
protected _configInitialized => true
}

Here’s something I don’t understand. The classname is '‘DebugKit.DebugKit’ instead of ‘smtp’. And it keeps being that way, even if I change my settings to this:

'gmail' => [
    'host' => 'smtp.gmail.com',
    'port' => 587,
    'username' => 'sabasco.development@gmail.com',
    'password' => 'mypassword',
    'className' => \Cake\Mailer\Transport\SmtpTransport::class,
    'tls' => true
],

Do you think this has something to do with my problem? Boy, I do hope so!

This is normal, I have that config value too for my GMail Mailer when I debug into it.

You are pretty deep in there in that SmtpTransport class because the _smtpSend() is to send literally 1 SMTP command line. So depending on what command is currently being sent you get different responses from the SMTP server.

But if you want you can disable the DebugKit plugin inside your Application.php and check again.

Even though the SMTP connection seems valid: Have you tried doing your mail sending from another network? So if you have a local setup you can try to send mails via your mobile phone hotspot.

1 Like

Hi @KevinPfeifer!
I uploaded my application to a production environment. Still: I’ve got the same error when I send the email:

SMTP Error: 530 5.7.0 Can't sign in to your Google Account - Gmail Help l9-20020a1709060cc900b007c0f2d051f4sm15412107ejh.203 - gsmtp

Via support.google.com I’ve found the exact error code and error description for SMTP Error 530, “5.7.0”:

Must issue a STARTTLS command first.

It’s a clue! I’ll keep searching how I can add a ‘STARTTLS command’ to my application config :wink:

Make sure your transport config has 'tls' => true just like my config above