Menu

Hi, welcome back to jaironlanda.com. Today I will show you how to using email class or library in Codeigniter 3. It is pretty simple and easy to learn if you have basic about PHP programming language.

 

Step 1:

(If you want use your own controller you can skip this step)

Create new controller SendMail.php file inside application/controllers library.

 

Step 2:

We will using email library or class you need declare $this->load->library('email');

Enter following code inside your function:

    public function send()
    {
        //Load email library
        $this->load->library('email');
        //Initialize your email setting
        $config = array(
            'protocol'  => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => '465',
            'smtp_user' => 'xxxx', //your google email
            'smtp_pass' => 'xxxx', //your email password here
            'mailtype'  => 'html',
            'charset'   => 'iso-8859-1',
            'wordwrap'  => TRUE,
            'newline'   => "\r\n",
        );
        $this->email->initialize($config);
        $this->email->from('googleemailhere', 'yournamehere'); //First parameter This email address is being protected from spambots. You need JavaScript enabled to view it., second parameter enter your name.
        $this->email->to('sendtoemailhere'); //send to email
        //$this->email->cc(This email address is being protected from spambots. You need JavaScript enabled to view it.'); //Add another email (optional)
        //$this->email->bcc(This email address is being protected from spambots. You need JavaScript enabled to view it.'); //BCC another email (optional)
        
        $this->email->subject('Test Email library'); //Enter your subject
        $msg = "Hi, <br><br> This is tutorial about email library. Thank you for coming";
        $this->email->message($msg); // Enter your message here.
        
        $status = $this->email->send();
        
        if ($status == true) {
            echo 'success!';        
        } else {
            echo 'error send email';
            echo $this->email->print_debugger();        
        }
        
        
    }

You can get Google smtp setting here

Save it and test it.

That it. If you have problem about this topic just comment bellow, I will try help you. :D