geeth
geeth

Reputation: 714

CodeIgniter email throws error

I cannot send mail using CodeIgniter. It displays the following error.

fwrite(): send of 28 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host.

This my settings in email.php library

public $useragent   = 'CodeIgniter';
public $mailpath    = '/usr/sbin/sendmail'; // Sendmail path
public $protocol    = 'smtp';       // mail/sendmail/smtp
public $smtp_host   = 'smtp.mailhostbox.com';
public $smtp_user   = '[email protected]';
public $smtp_pass   = 'xxxxxx';
public $smtp_port   = 25;
public $smtp_timeout    = 5;
public $smtp_keepalive  = FALSE;
public $smtp_crypto = '';
public $newline     = "\r\n";

I could send email till yesterday. But from today morning, it shows this error.
UPDATE : I have changed my host to gmail , but still not working. The following are the changes I made

public $useragent   = 'CodeIgniter';
public $mailpath    = '/usr/sbin/sendmail'; // Sendmail path
public $protocol    = 'smtp';       // mail/sendmail/smtp
public $smtp_host   = 'smtp.gmail.com';
public $smtp_user   = '[email protected]';
public $smtp_pass   = 'xxxxxx';
public $smtp_port   = 465;
public $smtp_timeout    = 5;
public $smtp_keepalive  = FALSE;
public $smtp_crypto = 'ssl';
public $newline     = "\r\n";

But it shows error as
Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465:465 (Failed to parse address "smtp.gmail.com:465:465")

I changed the port from 465 to 587 as follows

public $smtp_port   = 587;
public $smtp_crypto = 'tls';

But I got this error

Message: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed

I couldn't understand what is the real issue, because it throws errors in every possible conditions

Upvotes: 2

Views: 2026

Answers (1)

Gorakh Yadav
Gorakh Yadav

Reputation: 302

You need to initialise the config.see here

For Example

function send_email($attributes) {

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

        $this->email->set_newline("\r\n");

        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'host';
        $config['smtp_port'] = '465';
        $config['smtp_user'] = '[email protected]';
        $config['smtp_from_name'] = 'FROM NAME';
        $config['smtp_pass'] = 'XXX';
        $config['wordwrap'] = TRUE;
        $config['newline'] = "\r\n";
        $config['mailtype'] = 'html';                       

        $this->email->initialize($config);

        $this->email->from($config['smtp_user'], $config['smtp_from_name']);
        $this->email->to($attributes['to']);
        $this->email->cc($attributes['cc']);
        $this->email->bcc($attributes['cc']);
        $this->email->subject($attributes['subject']);

        $this->email->message($attributes['message']);

        if($this->email->send()) {
            return true;        
        } else {
            return false;
        }       

}

Upvotes: 1

Related Questions