Codeigniter Sending email via SMTP Gmail

Here I share with you the way; How to send email in Codeigniter Framework via Gmail SMTP feature.

Write this code in your controller:-

Controller

public function send_email() {

        $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => 465,
            'smtp_user' => 'YOUR_GMAIL_ID@gmail.com', // gmail Username
            'smtp_pass' => 'YOUR_GMAIL_PASSWORD', // gmail Password
            'mailtype' => 'html', // what type of mail you want to sent. i.e html/plaintext
            'charset' => 'utf-8',
            'wordwrap' => TRUE
        );

        $message = "Hi, This is a test mail using gmail smtp from my PC using CodeIgniter Framework.";

        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");
        $this->email->from('FROM_EMAIL_ADDRESS'); // in which email address you want to send email.
        $this->email->to('TO_EMAIL_ADDRESS'); // where your mail goes.
        $this->email->subject('Test Email via Gmail SMTP from My PC');
        $this->email->message($message);
        $success = $this->email->send();

        if ($success) {
            echo 'Waooo.. Email send successfully!!!';
        } else {
            // if email don't send then shows error.
            show_error($this->email->print_debugger());
        }
    }