Send email using Codeigniter email library

In this post, I would like to show how to send email using codeigniter email library. Codeigniter supported inbuilt library class. You just need to load library and this will make easy to sending emails.
1.) Create a view file called email inside myapp/application/views.

<html>
<head>
<title>Email using Codeigniter library</title>
</head>
<body>
<style>
h3{
font-family: Verdana;
font-size: 18pt;
font-style: normal;
font-weight: bold;
color:red;
text-align: center;
}
table{
font-family: Verdana;
color:black;
font-size: 12pt;
font-style: normal;
font-weight: bold;
text-align:left;
border-collapse: collapse;
}
.error{
    color:red;
  font-size: 11px;
}
</style>
<h3>Send Email Using Codeigniter Library</h3>
<?php echo form_open('email/send',array('name'=>'email')); ?>
    <table align="center" cellpadding = "5">
        <tr>
            <td colspan="5" align="center">
            <input type="submit" name="send" value="Send Email"/></td>
        </tr>
    </table>
<?php echo form_close();?>
</body>
</html>

2.) Create a controller called EmailController inside myapp/application/controllers to put all send email functionality.Please find below the code.
EmailController.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class EmailController extends CI_Controller {
publicfunction __construct()
{
parent::__construct();
$this->load->library('email');
}
    public function index(){
        $this->load->view('email');
    }
publicfunction send(){
$message_body = "Hello<br><br>Greetings!<br><br>You are just a step away from accessing your account.<br><br>We are sharing a Login credentials to access your account.<br><br>Once you Login, you'll be prompted to set a new password immediately. This is to ensure that only you have access to your account.";
$this->email->from('hitesh.maity@gmail.com','Techalltype'); /*// The email address and name of the person sending the email */
$this->email->to('hitesh.maity@gmail.com'); /*// The email address(s) of the recipient(s). Can be a single email , a list separated with comma or an array */
$this->email->subject('Techalltype : Login Credentials');
$this->email->message($message_body);
$this->email->send();
}
}

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – let us know. We’d love to help!

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.