获取错误fsockopen():使用codeigniter电子邮件类发送电子邮件时无法连接到smtp.googlemail.com:465(连接超时)

时间:2017-01-23 19:10:05

标签: php codeigniter email

我是codeigniter的新手我正在尝试使用codeigniter电子邮件类发送电子邮件,但我收到错误fsockopen():无法连接到smtp.googlemail.com:465(连接超时)我试图解决此问题,但没有运气

public function signup()
{
    $this->form_validation->set_rules('email','Email','required|valid_email|is_unique[fbadmin.email]');
    $this->form_validation->set_error_delimiters("<p class='text-danger'>","</p>");
    if( $this->form_validation->run()==TRUE )
    {
        $data= $this->input->post();
        unset($data['submit']);
        //print_r($data);exit;
        $this->load->model('Fbadminmodel');
        if($this->Fbadminmodel->signup($data))
        {
            //$this->load->library('email');
            $config = array(
                'protocol'  =>'smtp',
                'smtp_host' =>'smtp.googlemail.com',
                'smtp_port' =>'465',
                'smtp_user' =>'anujk3313@gmail.com',
                'smtp_pass' =>'password',
                'mail_type' =>'html',
                'charset'   =>'utf-8'
                );
            //$this->email->initialize($config);
            $this->load->library('email', $config);
            $this->email->from('anujk3313@gmail.com','Anuj Kumar');
            $this->email->to($data);
            $this->email->message('www.haiviral.com');
            $this->email->set_newline("\r\n");

            if($this->email->send())
            {
                //$this->session->set_flashdata('feedback','Succefully Registred. Please verify your email');
                echo "mail send";
            }
            else
            {
                show_error($this->email->print_debugger());
            }
        }
        else
        {
            $this->load->view('fbadmin');
            $this->session->set_flashdata('feedback','Registration Failed');
        }
        //$this->session->flashdata('flash','Email Sent');
        $this->load->view('fbadmin');
    }
    else
    {
        echo validation_errors();
        $this->load->view('fbadmin');
    }
}

2 个答案:

答案 0 :(得分:0)

有些事情可能是错的。我要做的第一件事是设置smtp_timeout设置:

$config = array(
    'smtp_timeout'=>'30', //<-- add this
    'protocol'  =>'smtp',
    'smtp_host' =>'smtp.googlemail.com',
    'smtp_port' =>'465',
    'smtp_user' =>'anujk3313@gmail.com',
    'smtp_pass' =>'password',
    'mail_type' =>'html',
    'charset'   =>'utf-8'
);

接下来我会检查您的托管是否允许您访问端口465:

$fp = fsockopen("smtp.gmail.com", 465, $errno, $errstr, 10);
if (!$fp)
    echo "smtp.gmail.com 465  -  $errstr   ($errno)<br>\n";

如果仍然无效,我会尝试禁用gmail的两步验证。

答案 1 :(得分:0)

添加ssl

$emailConfig = [
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'xxx@gmail.com',
        'smtp_pass' => 'xxx',
        'mailtype' => 'html',
        'charset' => 'iso-8859-1'
    ];