2

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   = 'xxx@xx.in';
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   = 'noreply.xxx@gmail.com';
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

geeth
  • 704
  • 2
  • 14
  • 42
  • Change `$smtp_port` value to `465` – Himanshu Upadhyay Dec 11 '17 at 07:15
  • i changed port to `465` but i get some other error as `fsockopen(): unable to connect to smtp.mailhostbox.com:465 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. )` – geeth Dec 11 '17 at 07:19
  • I hope you have checked this one https://stackoverflow.com/questions/13694351/error-while-sending-an-email-with-codeigniter – Himanshu Upadhyay Dec 11 '17 at 07:36
  • I have checked and suggestions listed there didn't work for me – geeth Dec 11 '17 at 07:42
  • did you initialise the config ? – Gorakh Yadav Dec 11 '17 at 12:15
  • I think he pasted the default settings from the Email library file itself. So it should be initialized? Also, did you change anything in your code between the working and not working state? It seems like something might have changed on the SMTP server side. – Fjarlaegur Dec 11 '17 at 14:24
  • @Clemenz Nothing chenged. I checked it with my network administrator. – geeth Dec 12 '17 at 06:49
  • please check my update – geeth Dec 12 '17 at 07:03

1 Answers1

1

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'] = 'user@smtp.com';
        $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;
        }       

}
Gorakh Yadav
  • 304
  • 5
  • 19
  • `$this->email->set_newline("\r\n");` is redundant to this line: `$config['newline'] = "\r\n";` I would prefer the second line, as you set all the other parameters this way. – Fjarlaegur Dec 11 '17 at 14:26
  • i have already set all these parameters in`email library`. And load this file using `auotoload.php` – geeth Dec 12 '17 at 06:48