This is just a simple PHP code that uses CodeIgniter framework to send email via SMTP protocal and charset UTF-8.
We can apply this code in a CodeIgniter’s controller that handles function need to send email to customers such as: activation registration, forgot password, sending notifications, etc.
It should work with almost SMTP services such as Google SMTP, Mandrill or even your own SMTP. In my project, I used Mandrill to send email to my customers.
Send Email Via SMTP Charset UTF-8 in CodeIgniter framework
Assume that we create a controller file named Auth.php in controllers folder.
controllers/auth.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Auth extends CI_Controller { public function __construct() { parent::__construct(); } public function send_email() { $to = "[email protected]";//need to change $subject = "Email subjet here"; $from_name = "4 Rapid Development"; $from_email = "[email protected]"; $body = ""; $body .= "Hi,<br>"; $body .= "How are you?<br><br>"; $body .= "<a href='http://4rapiddev.com'>4rapiddev.com</a>"; $this->load->library('email'); $config['charset'] = 'utf-8'; $config['mailtype'] = 'html'; $config['wordwrap'] = TRUE; $config['protocol'] = 'smtp'; $config['smtp_port'] = '25';//need to change; Mandrill: 587 $config['smtp_host'] = 'smtp.domain.com';//need to change; Mandrill: smtp.mandrillapp.com $config['smtp_user'] = "[email protected]";//need to change $config['smtp_pass'] = "smtp_password";//need to change $config['smtp_timeout'] = "30"; $this->email->initialize($config); $this->email->from('[email protected]', '4 Rapid Development'); $this->email->to($to); $this->email->subject($subject); $this->email->message($body); $this->email->send(); } } |
And if we want to separate config values in a config file (which should be located at config folder), simple create a new file named email.php and save it config folder then add all config values in that file.
Now, our files need to be changed as below:
config/email.php
1 2 3 4 5 6 7 8 9 10 11 | <?php $config['charset'] = 'utf-8'; $config['mailtype'] = 'html'; $config['wordwrap'] = TRUE; $config['protocol'] = 'smtp'; $config['smtp_port'] = '25';//need to change; Mandrill: 587 $config['smtp_host'] = 'smtp.domain.com';//need to change; Mandrill: smtp.mandrillapp.com $config['smtp_user'] = "[email protected]";//need to change $config['smtp_pass'] = "smtp_password";//need to change $config['smtp_timeout'] = "30"; ?> |
controllers/auth.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Auth extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('users_model'); } public function send_email() { $to = "[email protected]"; $subject = "Email subjet here"; $from_name = "4 Rapid Development"; $from_email = "[email protected]"; $body = ""; $body .= "Hi,<br>"; $body .= "How are you?<br><br>"; $body .= "<a href='http://4rapiddev.com'>4rapiddev.com</a>"; $this->load->library('email'); $this->email->from('[email protected]', '4 Rapid Development'); $this->email->to($to); $this->email->subject($subject); $this->email->message($body); $this->email->send(); } } |