为什么codeigniter电子邮件库不发送到Gmail帐户

时间:2014-03-21 16:51:04

标签: php codeigniter email

我有以下内容发送电子邮件

if($this->form_validation->run())
    {
        $data = $this->input->post();
        $this->load->library('email');
        $this->email->from('me@gmail.com', 'Joe Dev');
        $this->email->reply_to('me@gmail.com', 'Joe Dev');
        $this->email->to('someone@mail.com'); 
        $this->email->cc('friend@mail.com');

        $this->email->subject('Email Test');
        $this->email->message('I used this firstname: '. $data['fname']);   

        $this->email->send();

        $config['charset'] = 'iso-8859-1';
        $config['smtp_host'] = 'mail.google.com';
        $config['smtp_user'] = 'me@gmail.com';
        $config['smtp_pass'] = 'mypassword';

        $this->email->initialize($config);
    }

其中$ this-> email-> to()是我的网络邮件帐户,它成功传递邮件但是我尝试将其发送到的每个Gmail帐户都失败了。可能是什么问题?

1 个答案:

答案 0 :(得分:2)

在使用$this->email->send()之前,您必须初始化电子邮件库。     所以更正的代码将是:

    if($this->form_validation->run())
        {
            $data = $this->input->post();
            $this->load->library('email');
            $this->email->from('me@gmail.com', 'Joe Dev');
            $this->email->reply_to('me@gmail.com', 'Joe Dev');
            $this->email->to('someone@mail.com'); 
            $this->email->cc('friend@mail.com');
            $this->email->subject('Email Test');
            $this->email->message('I used this firstname: '. $data['fname']); 
            $config['charset'] = 'iso-8859-1';
            $config['smtp_host'] = 'mail.google.com';
            $config['smtp_user'] = 'me@gmail.com';
            $config['smtp_pass'] = 'mypassword';
            $this->email->initialize($config);
            $this->email->send();
        }

希望这有帮助!