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

**URL:** <https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869>\
**Category:** Need Help\
**Created:** [December 29, 2022, 3:18pm UTC](https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869 "2022-12-29T15:18:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![samclauw](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@samclauw](https://discourse.cakephp.org/u/samclauw)\
**Post date:** [December 29, 2022, 3:18pm UTC](https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869/1 "2022-12-29T15:18:23Z")

</div>

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](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/](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:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/a/a3a182fbd58976692c6723fea39493d35bcf7879.png)

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](https://book.cakephp.org/4/en/core-libraries/email.html))

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

```auto
$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:

```auto
'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:

```auto
'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:

```auto
'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 ☹

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 🙂  
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

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [December 29, 2022, 9:31pm UTC](https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869/2 "2022-12-29T21:31:39Z")

</div>

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

```auto
'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

```auto
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

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

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

        // rest of index action code
    }
}

```

---

<div class="post-metadata">

**Author:** ![samclauw](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@samclauw](https://discourse.cakephp.org/u/samclauw)\
**Post date:** [December 30, 2022, 2:00pm UTC](https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869/3 "2022-12-30T14:00:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [December 30, 2022, 2:15pm UTC](https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869/4 "2022-12-30T14:15:06Z")

</div>

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

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/9/93189e90651496f4d78bb2c68e7fbf458dff6106.png)

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

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [December 30, 2022, 3:06pm UTC](https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869/5 "2022-12-30T15:06:45Z")

</div>

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](https://myaccount.google.com/u/1/lesssecureapps)

All I have is that app password.

---

<div class="post-metadata">

**Author:** ![samclauw](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@samclauw](https://discourse.cakephp.org/u/samclauw)\
**Post date:** [January 3, 2023, 3:12pm UTC](https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869/6 "2023-01-03T15:12:48Z")

</div>

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:

```auto
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:

```auto
debug($this->_socket());

```

This results in:

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

```auto
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:

```auto
'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!

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [January 3, 2023, 4:24pm UTC](https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869/7 "2023-01-03T16:24:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![samclauw](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@samclauw](https://discourse.cakephp.org/u/samclauw)\
**Post date:** [January 4, 2023, 7:41pm UTC](https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869/8 "2023-01-04T19:41:39Z")

</div>

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](https://support.google.com/mail/?p=WantAuthError) l9-20020a1709060cc900b007c0f2d051f4sm15412107ejh.203 - gsmtp

Via [support.google.com](https://support.google.com/a/answer/3726730?hl=en) 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 😉

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [January 5, 2023, 11:43am UTC](https://discourse.cakephp.org/t/authentication-error-while-sending-emails-with-gmail-smtp-server-in-cakephp-4/10869/9 "2023-01-05T11:43:07Z")

</div>

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