CodeIgniter can't send email on hosted server

I'm currently working on a REST API using CodeIgniter. And I got an email verification system using the gmail smtp working just fine on localhost. But when I uploaded it to the hosted server it doesn't work.

Here's my code:

protected function send_confirmation($email, $hash){
    $config = array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.gmail.com',
        'smtp_port' => 465,
        'smtp_user' => 'xxx@gmail.com',
        'smtp_pass' => 'xxx',
        'mailtype'  => 'html',
        'charset'   => 'utf-8',
        'crlf' => "\r\n",
        'newline' => "\r\n"
    );

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

    $mail = $this->email;

    $mail->from('no-reply@example.com', 'Example.com');
    $mail->to($email);

    $mail->subject('Email Verification');

    $encodemail = urlencode($email);
    $message = 'Some messages here.';

    $mail->message($message);   

    $mail->send();
}

And here's the error message:

fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Network is unreachable)

Also note that I'm using PHP 5.6, and I've tried many configurations but nothing seems to work.

1 answer

  • answered 2018-05-16 04:44 Abdulla Nilam

    try these

    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'smtp.gmail.com', #changed
        'smtp_port' => 587, #changed 
        'smtp_user' => 'xxx@gmail.com',
        'smtp_pass' => 'xxx',
        'mailtype'  => 'html',
        'charset'   => 'utf-8',
        'newline' => "\r\n"
    );
    

    or use PHPMailer