Sending Email via SMTP Server in CodeIgniter

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.example.com',
    
'smtp_port' => 465,
    
'smtp_user' => 'email@example.com',
    
'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('recipient@example.com');
$this->email->from('sender@example.com','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.
    send-email-php-gmail-smtp-account-settings-codexworld
  • Scroll down the Password & sign-in method section and turn Off the 2-Step Verification.
    send-email-php-gmail-smtp-off-2-step-verification-codexworld
  • Scroll down the Connected apps & sites section and turn On Allow less secure apps.
    send-email-php-gmail-smtp-allow-less-secure-apps-codexworld

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.googlemail.com',
    
'smtp_port' => 465,
    
'smtp_user' => 'user@gmail.com',
    
'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('recipient@example.com');
$this->email->from('sender@example.com','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 the CodeIgniter Encrypt library before sending the email. It will encrypt your email and help to avoid the spamming issue in Gmail.

$this->load->library('encrypt');

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

12 Comments

  1. Agniswar Said...
  2. Faris Said...
  3. Oluwashola Babalola Said...
  4. Anand Kumar Said...
  5. Ramon Henry Said...
    • CodexWorld Said...
  6. Ramon Henry Said...
  7. No Name Said...
  8. Shaun Khan Said...
  9. Suzan Said...
    • CodexWorld Said...

Leave a reply

keyboard_double_arrow_up