Sending Email using SMTP codeigniter
The code is perfect.
You need to use Application password of gmail not the gmail password. Please set a application password from gmail settings and give the password here.
It will work perfectly then. See More from HERE
Process to setting up a app password in gmail:
- Visit your App passwords page. You may be asked to sign in to your Google Account.
- At the bottom, click Select app and choose the app you’re using.
- Click Select device and choose the device you’re using.
- Select Generate.
- Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
- Select Done.
Once you are finished, you’ll won’t see that App password code again. However, you will see a list of apps and devices you’ve created App passwords for.
Try this..
Create a file named 'email.php' inside 'application/config' folder and add the below settings to it.
<?php $config['protocol'] = 'smtp'; $config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this $config['smtp_port'] = '465'; $config['smtp_user'] = 'user@gmail.com'; //change this $config['smtp_pass'] = 'password'; //change this $config['mailtype'] = 'html'; $config['charset'] = 'iso-8859-1'; $config['wordwrap'] = TRUE; $config['newline'] = "\r\n";?>
In Controller:
<?php//send mailfunction sendmail(){ $this->load->library('email'); // load email library $this->email->from('user@gmail.com', 'sender name'); $this->email->to('test1@gmail.com'); $this->email->cc('test2@gmail.com'); $this->email->subject('Your Subject'); $this->email->message('Your Message'); $this->email->attach('/path/to/file1.png'); // attach file $this->email->attach('/path/to/file2.pdf'); if ($this->email->send()) echo "Mail Sent!"; else echo "There is error in sending mail!";}?>