How to set up email with a gmail account from codeigniter now that Google has disabled "less secure app" setting?

1.4k Views Asked by At

All the documentation I'm seeing for sending emails from a gmail account specify to check the "allow less secure apps to access" setting in your google account. Apparently as of last week, Google has disabled that setting, at least for accounts with 2 step authentication (this was required for me, as I'm using a custom domain as an alias for a gmail address). The workaround I'm seeing is to create a one-time "app password" from google and set it as 'mail_password'. I'm trying switching around ssl/tls, as well as using the @gmail.com address name instead of the alias, and have had no success in getting the confirmation email to send after booking on a hosted installation of easy appointments.

I'm not seeing errors showing up in the console, is there a place where I should be able to see the errors/what exactly its problem is? Do I need a 'useragent'? Am I missing any necessary variables? Thanks,

application/config/email.php

<?php defined('BASEPATH') or exit('No direct script access allowed');

// $config['useragent'] = 'Easy!Appointments';
$config['protocol'] = 'smtp'; // or 'smtp'
$config['smtp_port'] = 587;
$config['mailtype'] = 'html'; // or 'text'
// $config['smtp_debug'] = '0'; // or '1'
// $config['smtp_auth'] = TRUE; //or FALSE for anonymous relay.
$config['smtp_host'] = 'smtp.gmail.com';
$config['mail_password']='<gmail one time app password>';
$config['smtp_user'] = '[email protected]'; //set up as an alias of a gmail account
$config['smtp_pass'] = '<gmail password>';
$config['smpt_timeout'] = 60;
$config['smtp_crypto'] = 'ssl'; // or 'tls'
2

There are 2 best solutions below

2
On

I was able to find the error message incase of email sending failed cases like this.

if($this->email->send()){
echo json_encode($data);
}else{
$data['status'] = $this->email->print_debugger(); //failed case
echo json_encode($data);
}

Also, when I copied the app password from the google console, it has added spaces in between each character, which caused the failure. I have removed the spaces and it worked well. Hope this helps.

0
On

Since 30 May'22 Google has disabled "allow less secure app", I used another way as follows:

  • enable 2FA (I used the mobile number and code from Google Authenticator)
  • create app passwords (if you are not aware of how, you can see the link below)
  • enable IMAP in GMail settings (don't skip this step, because I still can't send email without it)

Finally, use app passwords in the smtp_pass parameter in the config email code in Codeigniter.

I previously took reference from https://help.warmupinbox.com/en/articles/4934806-configure-for-google-workplace-with-two-factor-authentication-2fa