Sending Email via SMTP Server in CodeIgniter ctcrowd

Using SMTP server is always a good idea to send email from the script. Sometimes PHP mail() function fails to send email to the recipient or deliver email to the spam folder. To avoid this issue SMTP is an effective way to send an email. CodeIgniter Email Class provides an easy way to send email from the PHP script. Also, you can send email via SMTP server using CodeIgniter Email library.

In this tutorial, we will show how you can send HTML email via SMTP server in CodeIgniter application. The CodeIgniter email library will be used to send email using SMTP server.

Send email via 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.

 //Load email library $this->load->library('email'); //SMTP & mail configuration $config = array(     'protocol'  => 'smtp',     'smtp_host' => 'ssl://',     'smtp_port' => 465,     'smtp_user' => '',     'smtp_pass' => 'email_password',     'mailtype'  => 'html',     'charset'   => 'utf-8' ); $this->email->initialize($config); $this->email->set_mailtype("html"); $this->email->set_newline("\r\n"); //Email content $htmlContent = '<h1>Sending email via SMTP server</h1>'; $htmlContent .= '<p>This email has sent via SMTP server from CodeIgniter application.</p>'; $this->email->to(''); $this->email->from('','MyWebsite'); $this->email->subject('How to send email via SMTP server in CodeIgniter'); $this->email->message($htmlContent); //Send email $this->email->send();

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.


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


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


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

The following example code help to send HTML email in CodeIgniter using your Gmail account. You only need to specify your Gmail email address (smtp_user) and password (smtp_pass).

 //Load email library $this->load->library('email'); //SMTP & mail configuration $config = array(     'protocol'  => 'smtp',     'smtp_host' => 'ssl://',     'smtp_port' => 465,     'smtp_user' => '',     'smtp_pass' => 'gmail_password',     'mailtype'  => 'html',     'charset'   => 'utf-8' ); $this->email->initialize($config); $this->email->set_mailtype("html"); $this->email->set_newline("\r\n"); //Email content $htmlContent = '<h1>Sending email via SMTP server</h1>'; $htmlContent .= '<p>This email has sent via SMTP server from CodeIgniter application.</p>'; $this->email->to(''); $this->email->from('','MyWebsite'); $this->email->subject('How to send email via SMTP server in CodeIgniter'); $this->email->message($htmlContent); //Send email $this->email->send(); 

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 rolex replica paypal the CodeIgniter Encrypt library before sending the email. It will encrypt your email and help to avoid the spamming issue in Gmail.



Leave a Reply