How to resolve the error: Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Connection refused)

只谈情不闲聊 提交于 2019-12-22 18:24:07

问题


I'm using codeigniter I created one email function to send email to particular email ID

public function email($email){
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'ssl://smtp.gmail.com';
        $config['smtp_timeout']=5;
        $config['smtp_port'] = '465';
        $config['smtp_user'] = 'MyEmailID@gmail.com';
        $config['smtp_pass'] = '********************';
        $config['mailtype'] = 'html';
        $config['charset'] = 'utf-8';
        $config['newline'] = "\r\n";
        $config['wordwrap'] = TRUE;
        $this->load->library('email');
        $this->email->initialize($config);

        $this->email->from('MyEmailID@gmail.com', 'MyEmailID');
        $this->email->to($email);
        $this->email->subject('My Subject');
        $this->email->message('My Message');
        return $this->email->send();
    }

That's working nice in my local machine but when I uploaded that code to hosting server (I'm using Godaddy hosting) I'm getting following error:

A PHP Error was encountered

Severity: Warning

Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Connection refused)

Filename: libraries/Email.php

Line Number: 2020

Backtrace:

File: /home/MyDir/public_html/application/models/BasicModel.php Line: 47 Function: send

File: /home/MyDir/public_html/application/controllers/Login.php Line: 48 Function: email

File: /home/MyDir/public_html/index.php Line: 315 Function: require_once


回答1:


Use Tls istead of ssl and change port number to 587 $this->email->initialize($config); $config = array();

     //Load email library 
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'tls://smtp.gmail.com';
        $config['smtp_port']    = '587';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 'email';
        $config['smtp_pass']    = 'password';
        $config['charset']    = 'utf-8';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';
        $config['validation'] = TRUE; // bool whether to validate email or not      



            $this->email->set_newline("\r\n");
            ;
            $this->email->clear(TRUE);
            $this->email->to($this->input->post('email'));
            $this->email->subject($row_email->email_subject);

working site




回答2:


You forgot to add '

change from

 $config['smtp_host'] = 'ssl://smtp.gmail.com';

to

$config['smtp_host'] = 'smtp.gmail.com';

or

Uncomment php_openssl.dll in php.ini



来源:https://stackoverflow.com/questions/41425107/how-to-resolve-the-error-message-fsockopen-unable-to-connect-to-ssl-smtp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!