Search

How to Send Email on the inbox in The CodeIgniter framework

  • Share this:
post-title

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

Load CodeIgniter Email library 

To send an email using CodeIgniter, first you have to load email library using the following −

$this->load->library('email');

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

<?php 
   class sendmail extends CI_Controller {
      
      //This is the index method, we will send an email by calling the Sendemail function in it.
     
     public function index(){
       $this->sendemail('[email protected]','text email','mail check','check');
     }
    //This is an email sending function 
     function sendemail($useremail,$message,$subject,$purpose){
        $this->load->library('email'); // have to load email library
        $config['protocol']     = "smtp";
        $config['smtp_host']    = "Smtp Host name";
        $config['smtp_port']    = 587;
        $config['smtp_user']    = "smtp user name"; 
        $config['smtp_pass']    = "smtp user password";
        $config['charset']      = "utf-8";
        $config['mailtype']     = "text";
        $config['wordwrap']     = TRUE;
        $this->email->initialize($config); //have to initialize smtp configration
        $this->email->set_mailtype("html");
        $this->email->from('smtp user name', 'purpose');
        $this->email->to('destination Address');
        $this->email->subject('subject');
        $this->email->message('message');
        $this->email->send();
     }

   } 
?>

Note :- In this you have to configure the SMTP Host ,SMTP  User , SMTP Password

About author
Here’s My little description of your attention on Me and My blog. I am here to help you with PHP programming.
View all posts (53)