How to send email in Codeigniter email Library

Codeigniter | Email | Library | PHP

Codeigniter email library


This entry is part 6 of 8 in the series Learn Codeigniter

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.

  • Email
  • 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.

Download Source Code Files

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.

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.

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(“”). 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

codeigniter email library output


Download Source Code Files


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.

Series Navigation<< CodeIgniter HelpersCodeigniter: Creating Custom Library in Codeigniter >>

Bookmark Link using: bookmark at folkd