Categories
PHP

Send Email in CodeIgniter With SMTP

These days, almost every PHP framework supports a well developed email management library. CodeIgniter is no different as it has a great email sending class that ensures that CodeIgniter projects could send emails without difficulty.

Send Email in CodeIgniter With SMTP

In this article, I will describe how to send emails in a CodeIgniter application using SMTP. I will use the well known and maintained email sending class.

Load CodeIgniter’s Email Class

First, load the CodeIgniter’s email library through the following code snippet:

  1. $this->load->library(’email’);

Set Email Parameters

The next step is to setup the required fields for the custom email. these fields could be setup through several functions including: from() function takes two parameters – the email address of the sender and the name. to() function takes the email address of the recipient. Next two functions are subject() and message() that round up the requirements for sending emails in CodeIgniter. Here is how these functions are used in the code:

  1. $this->email->from(’[email protected]’, ‘Identification’);
  2. $this->email->to(’[email protected]’);
  3. $this->email->subject(‘Send Email Codeigniter’);
  4. $this->email->message(‘The email send using codeigniter library’);

Once these functions are filled, the final step is to send the email by using the send() function.

  1. $this->email->send();

Create the Controller

Create a controller file Sendingemail_controller.php and save it in the application/controller/. Add the following code to this file:

  1. <?php
  2. class Sendingemail_Controller extends CI_Controller {
  3. function __construct() {
  4. parent::__construct();
  5. $this->load->library(‘session’);
  6. $this->load->helper(‘form’);
  7. }
  8. public function index() {
  9. $this->load->helper(‘form’);
  10. $this->load->view(‘contact_email_form’);
  11. }
  12. public function send_mail() {
  13. $from_email = “[email protected]”;
  14. $to_email = $this->input->post(’email’);
  15. //Load email library
  16. $this->load->library(’email’);
  17. $this->email->from($from_email, ‘Identification’);
  18. $this->email->to($to_email);
  19. $this->email->subject(‘Send Email Codeigniter’);
  20. $this->email->message(‘The email send using codeigniter library’);
  21. //Send mail
  22. if($this->email->send())
  23. $this->session->set_flashdata(“email_sent”,”Congragulation Email Send Successfully.”);
  24. else
  25. $this->session->set_flashdata(“email_sent”,”You have encountered an error”);
  26. $this->load->view(‘contact_email_form’);
  27. }
  28. }
  29. ?>

Create the View

Create a view file called contact_email_form.php and save it in application/views/. Add the following code to it:

  1. <html>
  2. <head>
  3. <title> Send Email Codeigniter </title>
  4. </head>
  5. <body>
  6. <?php
  7. echo $this->session->flashdata(’email_sent’);
  8. echo form_open(‘/Sendingemail_Controller/send_mail’);
  9. ?>
  10. <input type = “email” name = “email” required />
  11. <input type = “submit” value = “SEND MAIL”>
  12. <?php
  13. echo form_close();
  14. ?>
  15. </body>
  16. </html>

Make the changes in the routes.php file in application/config/routes.php and add the following line at the end of the file:

  1. $route[’email’] = ‘Sendingemail_Controller’;

Access the Email Application

Finally, Hit the following URL to access the application:

  1. http://your-domain.com/index.php/email

Setting SMTP Configuration

As mentioned earlier, CodeIgniter fully supports different email protocols including SMTP through simple configuration options.

As you could see from the following code snippet, selecting the email protocol is the matter of setting a single configuration variable. In this code snippet, I have set the $config[‘protocol’] to  smtp for using SMTP protocol.

  1. <?php
  2. $this->load->library(’email’);
  3. $config = array();
  4. $config[‘protocol’] = ‘smtp’;
  5. $config[‘smtp_host’] = ‘xxx’;
  6. $config[‘smtp_user’] = ‘xxx’;
  7. $config[‘smtp_pass’] = ‘xxx’;
  8. $config[‘smtp_port’] = 25;
  9. $this->email->initialize($config);
  10. $this->email->set_newline(“\r\n”);