Skip to content

How to Send Gmail using CodeIgniter Email Library

Last updated on October 15, 2016

In this post I would like to show you configuring Codeigniter email library to send emails using GMail SMTP server. Configuring and sending emails in Codeigniter application is not a tedious task, it is very simple and easy with few lines of code and config values you can send emails. You can set configuration options on fly or you can set config options of email library globally at applications/config/email.php.

Now I will explain how to send email using PHP and CodeIgniter Framework,you must load email library in order to send emails.

 'smtp', 
            'smtp_host' => 'ssl://smtp.googlemail.com', 
            'smtp_port' => 465, 
            'smtp_user' => '[email protected]', 
            'smtp_pass' => 'xxx', 
            'mailtype' => 'html', 
            'charset' => 'iso-8859-1'
        ];
        // Set your email information
        $from = [
            'email' => '[email protected]',
            'name' => 'arjun'
        ];
       
        $to = array('[email protected]');
        $subject = 'Your gmail subject here';
      //  $message = 'Type your gmail message here'; // use this line to send text email.
        // load view file called "welcome_message" in to a $message variable as a html string.
        $message =  $this->load->view('welcome_message',[],true); 
        // Load CodeIgniter Email library
        $this->load->library('email', $emailConfig);
        // Sometimes you have to set the new line character for better result
        $this->email->set_newline("\r\n");
        // Set email preferences
        $this->email->from($from['email'], $from['name']);
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);
        // Ready to send email and check whether the email was successfully sent
        if (!$this->email->send()) {
            // Raise error message
            show_error($this->email->print_debugger());
        } else {
            // Show success notification or other things here
            echo 'Success to send email';
        }
    }
}

Message: mail() [function.mail]: Failed to connect to mailserver at “ssl://smtp.googlemail.com” port 465,
verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set()

Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.googlemail.com:465
(Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP?)

If those error occurred, make sure extension=php_smtp.dll and extension=php_openssl.dll uncommented in php.ini .If you are using xampp make sure you change php.ini in xampp/apache/bin/php.ini. If php.ini doesn’t exists to that directory, copy php.ini from xampp/php/php.ini to xampp/apache/bin/.

$this->email->set_newline enclose with double quotes. This is very important or else it does not work.Because PHP interpreter will consider \n as new line only when it is in enclosed with double quotes and if it in single quotes interpreter consider it as the literal character. So have a eye on it.

0 0 votes
Article Rating
Subscribe
Notify of
guest

20 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments