Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP CodeIgniter Send Email Via SMTP Charset UTF-8

CodeIgniter Send Email Via SMTP Charset UTF-8

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();	
	}
}

<?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";
?>

<?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();	
	}
}

<?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(); } }

Oct 24, 2014Hoan Huynh
Codeigniter Pagination Show Total PagesFatal error: Using $this when not in object context
You Might Also Like:
  • Send Email Via Gmail SMTP Authentication With PHPMailer
  • PHPMailer Send Email With Attachments Via SMTP Authentication
  • PHPMailer Send Email HTML Content With UTF-8 Encoding
  • PHP Send Email To Multiple Recipients And CC To Multiple Recipients With PHP Mailer
  • Sending Email With SMTP Authentication In WordPress
  • Simple PHP Code Send Email
  • Codeigniter Pagination Show Total Pages
  • CodeIgniter Upload And ReSize Image Maintain Ratio
  • How To Send Google Analytics Report To Emails
  • CodeIgniter Save Or Log All MySQL Queries
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

7 years ago PHPCodeigniter, CodeIgniter Send Email, Google SMTP, Mandrill, Mandrill SMTP, SMTP, utf 81,770
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,550 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,886 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,740 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,063 views
C# Read Json From URL And Parse/Deserialize Json
11,795 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development