sending email via gmail smtp with codeigniter

Sending email with gmail smtp with codeigniter email library

Using SMTP server is always a good idea to send email from the codeigniter. while sending email without smtp(mail()), it may chance to deliver email to the spam folder. To avoid this issue SMTP is an better way to send an email. CodeIgniter have a inbulit Email Class provides to sent email.
In this tutorial, we will show how you can send HTML email via Gmail SMTP server in CodeIgniter application. The CodeIgniter email library will be used to send email using Gmail SMTP server.

Send email via Gmail SMTP server in CodeIgniter

At first include the CodeIgniter email library. Now specify the SMTP host (smtp_host), port (smtp_port), email (smtp_user), and password (smtp_pass) in SMTP configuration ($config) as per your SMTP server.

Send email via Gmail SMTP server in CodeIgniter

To use Gmail SMTP for sending email in CodeIgniter, you need to make some changes in Google account settings.
Follow the below steps to use Gmail SMTP in CodeIgniter email library.

Login to your Google account.
Go to the My Account page. Click the Signing in to Google link from Sign-in & security section.

sending email with gmail smtp with codeigniter email library

Scroll down the Password & sign-in method section and turn Off the 2-Step Verification.

send-email-php-gmail-smtp-off-2-step-verification-getsourceodes

Scroll down the Connected apps & sites section and turn On Allow less secure apps.

send-email-php-gmail-smtp-allow-less-secure-apps-getsourcecodes

Now your Gmail account is ready to use in CodeIgniter email library as an SMTP server.

If you notice that the emails are sent to the spam folder, use Encrypt Class in CodeIgniter to solve this issue in Gmail. You need to load the CodeIgniter Encrypt library before sending the email. It will encrypt your email and help to avoid the spamming issue in Gmail.

If you face any issue in above code.Please comment.Happy Coding…