查阅数据库中的电子邮件并发送

时间:2017-10-23 22:10:21

标签: php codeigniter

我正在尝试使用此功能发送电子邮件,如果我静态编写电子邮件,它可以正常工作,但在尝试从数据库中查询时, $ email_user 变量无法获取sql查询的值。我做错了什么?

模型

public function send(){

        $this->db->select('email_user');
        $this->db->from('users');
        $this->db->where('id_user= "8" ');
        $email_user = $this->db->get()->row();

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

        $this->email->from('xxxx@gmail.com');
        $this->email->to($email_user);
        $this->email->subject('example');
        $this->email->message('Hello World');
        $this->email->send();

}

1 个答案:

答案 0 :(得分:0)

确保您已设置了电子邮件配置,并尝试使用db

public function send(){

    $id = '8';

    $this->db->where('id_user', $id);
    $query = $this->db->get('users');

    $row = $query->row();

    $config = array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => '****',
        'smtp_pass' => '****',
        'mailtype'  => 'html', 
        'charset'   => 'iso-8859-1'
    );

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");

    $this->email->from('xxxx@gmail.com', 'Admin');
    $this->email->to($row->email_user);
    $this->email->subject('example');
    $this->email->message('Hello World');
    $this->email->send();

}