Sending email with gmail smtp with codeigniter email library

class Email extends Controller {

    function Email()

    function index()
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'ssl://';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = '';
        $config['smtp_pass']    = '*******';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'text'; // or html
        $config['validation'] = TRUE; // bool whether to validate email or not      


        $this->email->from('', 'myname');

        $this->email->subject('Email Test');
        $this->email->message('Testing the email class.');  


        echo $this->email->print_debugger();


I am getting this error:

A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: unable to connect to ssl:// (Connection timed out)
Filename: libraries/Email.php
Line Number: 1641

Using PORT 25/587

I got this error:

A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:func(119):reason(252)
Filename: libraries/Email.php
Line Number: 1641

I don't want to use phpmailer now. (Actually I have tried to use phpmailer, but I failed).

How do I solve this problem guys?


$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://',
    'smtp_port' => 465,
    'smtp_user' => 'xxx',
    'smtp_pass' => 'xxx',
    'mailtype'  => 'html', 
    'charset'   => 'iso-8859-1'
$this->load->library('email', $config);

// Set to, from, message, etc.

$result = $this->email->send();

From the CodeIgniter Forums

You need to enable SSL in your PHP config. Load up php.ini and find a line with the following:


Uncomment it. :D

(by removing the semicolon from the statement)


According to the CI docs (CodeIgniter Email Library)...

If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the email.php, add the $config array in that file. Then save the file at config/email.php and it will be used automatically. You will NOT need to use the $this->email->initialize() function if you save your preferences in a config file.

I was able to get this to work by putting all the settings into application/config/email.php.

$config['useragent'] = 'CodeIgniter';
$config['protocol'] = 'smtp';
//$config['mailpath'] = '/usr/sbin/sendmail';
$config['smtp_host'] = 'ssl://';
$config['smtp_user'] = '';
$config['smtp_pass'] = 'YOURPASSWORDHERE';
$config['smtp_port'] = 465; 
$config['smtp_timeout'] = 5;
$config['wordwrap'] = TRUE;
$config['wrapchars'] = 76;
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['validate'] = FALSE;
$config['priority'] = 3;
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
$config['bcc_batch_mode'] = FALSE;
$config['bcc_batch_size'] = 200;

Then, in one of the controller methods I have something like:

$this->load->library('email'); // Note: no $config param needed
$this->email->from('', '');
$this->email->subject('Test email from CI and Gmail');
$this->email->message('This is a test.');

Also, as Cerebro wrote, I had to uncomment out this line in my php.ini file and restart PHP:


Change it to the following:

$ci = get_instance();
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://";
$config['smtp_port'] = "465";
$config['smtp_user'] = ""; 
$config['smtp_pass'] = "yourpassword";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";


$ci->email->from('', 'Blabla');
$list = array('');
$this->email->reply_to('', 'Explendid Videos');
$ci->email->subject('This is an email test');
$ci->email->message('It is working. Great!');

send html email via codeiginater


    $config['mailtype'] = "html";
    $this->email->from('', 'Website');
    $list = array('', '');
    $data = array();
    $htmlMessage = $this->parser->parse('messages/email', $data, true);
    $this->email->subject('This is an email test');

    if ($this->email->send()) {
        echo 'Your email was sent, thanks chamil.';
    } else {

Another option I have working, in a linux server with Postfix:

First, configure CI email to use your server's email system: eg, in email.php, for example

# alias to postfix in a typical Postfix server
$config['protocol'] = 'sendmail'; 
$config['mailpath'] = '/usr/sbin/sendmail'; 

Then configure your postfix to relay the mail to google (perhaps depending on the sender address). You'll probably need to put you user-password settings in /etc/postfix/sasl_passwd (docs)

This is much simpler (and less fragmente) if you have a linux box, already configured to send some/all of its outgoing emails to Google.

Perhaps your hosting server and email server are located at same place and you don't need to go for smtp authentication. Just keep every thing default like:

$config = array(        
    'protocol' => '',
    'smtp_host' => '',
    'smtp_port' => '',
    'smtp_user' => '',
    'smtp_pass' => '**********'


$config['protocol'] = '';
$config['smtp_host'] = '';
$config['smtp_port'] = ;
$config['smtp_user'] = '';
$config['smtp_pass'] = 'password';

it works for me.

It can be this:

If you are using cpanel for your website smtp restrictions are problem and cause this error. SMTP Restrictions

Error while sending an email with CodeIgniter

Need Your Help

How to throw an exception in C?

c exception syntax

I typed this into google but only found howtos in C++,

Linker errors when compiling against glib...?

c compilation linker

I'm having trouble compiling a simple, sample program against glib on Ubuntu. I get the following errors. I can get it to compile but not link with the -c flag, which I believe means I have the glib