如何解决该错误:消息:fsockopen():无法连接到ssl://smtp.gmail.com:465(连接被拒绝)

时间:2017-01-02 10:32:33

标签: php codeigniter email ssl

我正在使用 codeigniter 我创建了一个电子邮件功能来发送电子邮件到特定的电子邮件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();
    }

这在我的本地计算机上工作得很好,但当我将该代码上传到托管服务器我正在使用 Godaddy 托管时)我我得到以下错误:

  

遇到PHP错误

     

严重性:警告

     

消息:fsockopen():无法连接到ssl://smtp.gmail.com:465   (连接被拒绝)

     

文件名:libraries / Email.php

     

行号:2020

     

回溯:

     

文件:/home/MyDir/public_html/application/models/BasicModel.php   行:47功能:发送

     

文件:/home/MyDir/public_html/application/controllers/Login.php   行:48功能:电子邮件

     

文件:/home/MyDir/public_html/index.php行:315功能:   require_once

3 个答案:

答案 0 :(得分:1)

使用Tls代替ssl,并将端口号更改为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

答案 1 :(得分:0)

更改自:

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

收件人:

$config['smtp_host'] = 'mail.seuDominio.com';
$config['smtp_port'] = 587; //587
$config['smtp_user'] = 'seu@dominio.com';
$config['smtp_pass'] = 'suaSenha';

更改自:

$config['protocol']  = 'smtp'; 

收件人:

$config['protocol']  = 'mail';
$config['validate']  = TRUE;
$config['mailtype']  = 'html';
$config['charset']   = 'utf-8';
$config['newline']   = "\r\n";

答案 2 :(得分:-1)

您忘了添加'

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

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

取消注释php.ini中的php_openssl.dll