Codeigniter email library: Codeigniter comes with lots of built-in libraries. Which has lots of functionalities that we can use in our website development. Library in Codeigniter is basically a class which has several similar functionalities wrapped inside class. To use codeigniter library, one has to create an instance of that library. Codeigniter uses a special syntax to load library. But before we look into that let list out some useful libraries that codeigniter has.
- Pagination
- Session
- Cart
These are most important codeigniter libraries that we need in almost all websites except cart class. For example, email library contains functionality related to sending emails using sendmail or SMTP server, sending attachment with email and lot more. In these tutorial we will be looking into email library to understand how Codeigniter Libraries work.
Codeigniter Email Library
Step 1. Create a controller or use your existing controller. Put following code in either class constructor or in action method where you want to use email library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php class Home extends CI_Controller { function __construct() { parent::__construct(); } function index() { $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.googlemail.com', 'smtp_port' => 465, 'smtp_user' => 'yourusername@gmail.com', 'smtp_pass' => 'yourmailpass', 'mailtype' => 'html' ); $this->load->library('email', $config); } } |
We are using gmail SMTP service to send email in our codeigniter website using codeigniter email library. we have created an array for configuration about SMTP server and pass it in load library method. Codeigniter uses load library method to load any library that is available. First argument is library name (email) & second argument is array for initial configuration.
Step 2. Next step is to configure mail data like subject, from address, to address, body content, bcc etc. And then send mail. To do that add following highlighted code in controller that you are working in. Now remember this, when you load library it will be loaded inside $this object’s instance variable which is by default library name. So $this->email is now our instance of email library class.
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 |
<?php class Home extends CI_Controller { function __construct() { parent::__construct(); } public function index() { $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.googlemail.com', 'smtp_port' => 465, 'smtp_user' => 'yourusername@gmail.com', 'smtp_pass' => 'yourmailpass', 'mailtype' => 'html' ); $this->load->library('email', $config); $this->email->set_newline("\r\n"); // Set to, from, message, etc. $this->email->to("example@domain.com"); $this->email->from("example@domain.com","CodeRiddles Support"); $this->email->bcc("example2@domain.com"); $this->email->subject("Codeigniter email library Test"); $this->email->message("<b>Codeigniter email Library</b> Bodu Content"); $result = $this->email->send(); echo $this->email->print_debugger(); } } |
Codeigniter email library has many built-in functionalities like setting to, from, bcc, subject, message etc. we can set it like we did above using $this->email object. For example we just set to address using $this->email->to(“example@domain.com”). After setting to, from, subject, message, bcc We can call send method to finally send email. Send method will return object containing all information about email tracing. To check email is sent or not we can use codeigniter email library’s method print_debugger. Just like we did above. Check your mail for address which you have set in to method of codeigniter email library.
Output for print_debugger method
Your output will be little different then mine. Note the successfully sent message, thats important. If you want to know more about email library please feel free to mention your doubts in comments. We will be happy to solve your queries. Visit & like our Facebook Fan Page to get instant update of our website.
Pingback: Codeigniter: Creating Custom Library in Codeigniter » CodeRiddles