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.
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();
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.
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
Very good tutorial. How to encrypt email. It is going to spam folder. What we do after loading the encryption library?
Thx man, works well for me
This tutorial is clear and understanding. But how can one setup email gmail smtp that Gmail will recognise has secure and also how would this work on a live server using codeigniter
Very Help Full ! Worked for me and my website
@CodexWorld I saw the link you provided but I was asking if there is one with phpmailer and codeigniter came across a few on the web but they’re poorly done
We will try to publish your requested tutorial soon. Please subscribe our newsletter to get notified.
This is well done and put together. Could you do one using PHPMailer?
See this tutorial – https://www.codexworld.com/send-html-email-php-gmail-smtp-phpmailer/
yuuhu, nice one
Thanks for this article. There are other articles on using CI and SMTP, and yet others regarding using Google Mail SMTP servers, yet none of them are as complete as this one.
please make a tutorial on android and ios pushnotification in codeigniter.
We will try to publish your requested tutorial soon. Meanwhile, subscribe our newsletter and follow us on social media to get notified.