Search code examples
phpcodeignitersendmail

codeigniter shows stream_socket_client(): Failed to enable crypto this error on sendmail?


I am trying to send email with code igniter with xampp server on my local machine but its not working. it shows me some errors that i searched alot on net but i am helpless now all solutions that i tried are failed for me.

below are my 3 errors:

Message: stream_socket_client(): SSL: The operation completed successfully.

Message: stream_socket_client(): Failed to enable crypto

Message: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:587 (Unknown error)

this is my php code:

$config = Array(
        'protocol'  => 'smtp',
        'smtp_host' => 'ssl://smtp.gmail.com',
        'smtp_port' => '587',
        'smtp_user' => '[email protected]',
        'smtp_pass' => 'xxxmypassword',
        'mailtype'  => 'html',
        'starttls'  => true,
        'newline'   => "\r\n"
                    );
        $message = '';
        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");
        $this->email->initialize($config);
        $this->email->from('[email protected]');
        $this->email->to('[email protected]');
        $this->email->subject('Resume from JobsBuddy for your Job posting');
        $this->email->message($message);
      if($this->email->send())
        {
          echo 'Email sent.';
        }
     else
        {
         show_error($this->email->print_debugger());
        }

on print_debugger i get this error too:

The following SMTP error was encountered: 0 Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

This is a multi-part message in MIME format. Your email application may not support this format.

please guide me with proper solution.


Solution

  • You email config should look something like this:

    $config = Array(
      'protocol'  => 'smtp',
      'smtp_host' => 'ssl://smtp.gmail.com',
      'smtp_port' => 465,
      'smtp_user' => 'YOUR_USERNAME',
      'smtp_pass' => 'YOUR_PASSWORD',
      'mailtype'  => 'html',
      'charset'   => 'utf-8',
      'newline'   => "\r\n",
      'wordwrap'  => TRUE,
    );
    

    And like Sandeep J Patel said in his comment, you will also need to enable access for less secure apps in your gmail account settings.

    See their help page about that: https://support.google.com/accounts/answer/6010255?hl=en