CodeIgniter Send Email using SMTP

Installing CodeIgniter

If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter which is 3.1.7 that I've used in this tutorial. After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP. Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as codeigniter_email so I'm using the below code.
  1. localhost/codeigniter_email
codeigniter successful install

Configuring our Base URL

Next, we configure our base url to tell codeigniter that this is the URL of our site/application. We're gonna be using this a lot that's why we need to configure this. 1. In your codeigniter app folder, open config.php located in application/config folder. 2. Find and edit the ff line:
  1. $config['base_url'] = 'http://localhost/codeigniter_email';

Removing index.php in our Index

Next step is to remove index.php in all of our URLs. To do this, we need an .htaccess file. Please visit my tutorial How to remove index.php in CodeIgniter URL to further understand the steps.

Creating our Controller

Next, we are going to create our controller. Create a file named Email.php in application/controllers folder of our app and put the ff codes.
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Email extends CI_Controller {
  5. function __construct(){
  6. parent::__construct();
  7. $this->load->helper('url');
  8. $this->load->library('session');
  9. }
  10. public function index(){
  11. $this->load->view('email_form');
  12. }
  13. public function sendemail(){
  14. $subject = $this->input->post('subject');
  15. $message = $this->input->post('message');
  16. $email = $this->input->post('email');
  17.  
  18. $config = Array(
  19. 'protocol' => 'smtp',
  20. 'smtp_host' => 'ssl://smtp.googlemail.com',
  21. 'smtp_port' => 465,
  22. 'smtp_user' => '[email protected]', // change it to yours
  23. 'smtp_pass' => 'mysourcepass', // change it to yours
  24. 'mailtype' => 'html',
  25. 'charset' => 'iso-8859-1',
  26. 'wordwrap' => TRUE
  27. );
  28.  
  29. $this->load->library('email', $config);
  30. $this->email->set_newline("\r\n");
  31. $this->email->from($config['smtp_user']); // change it to yours
  32. $this->email->to($email);// change it to yours
  33. $this->email->subject($subject);
  34. $this->email->message($message);
  35. if($this->email->send()){
  36. $this->session->set_flashdata('message', 'Email sent');
  37. }
  38. else{
  39. $this->session->set_flashdata('message', show_error($this->email->print_debugger()));
  40.  
  41. }
  42.  
  43. redirect('/');
  44.  
  45. }
  46. }

Defining our Default Controller

Next, we are going to set our default controller so that if no controller is defined, this default controller will be used. Open routes.php located in application/config folder and set the default route to our user controller.
  1. $route['default_controller'] = 'email';

Creating our Email Form

Lastly, we create the form where we get data to include in our email. Create the ff file inside application/views folder. email_form.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeIgniter Sending Email</title>
  6. <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">CodeIgniter Sending Email</h1>
  11. <div class="row">
  12. <div class="col-sm-4 col-sm-offset-4">
  13. <?php
  14. if($this->session->flashdata('message')){
  15. ?>
  16. <div class="alert alert-info text-center">
  17. <?php echo $this->session->flashdata('message'); ?>
  18. </div>
  19. <?php
  20. }
  21. ?>
  22. <form method="POST" action="<?php echo base_url(); ?>email/sendemail">
  23. <div class="form-group">
  24. <label>Subject:</label>
  25. <input type="text" class="form-control" name="subject">
  26. </div>
  27. <div class="form-group">
  28. <label>Message:</label>
  29. <textarea class="form-control" name="message"></textarea>
  30. </div>
  31. <div class="form-group">
  32. <label>Email:</label>
  33. <input type="text" class="form-control" name="email">
  34. </div>
  35. <button type="submit" class="btn btn-primary">Send</button>
  36. </form>
  37. </div>
  38. </div>
  39. </div>
  40. </body>
  41. </html>
That ends this tutorial. Happy Coding :)

Add new comment