如何从代码点火器休息控制器中的rest API发送邮件?

时间:2017-01-18 16:09:45

标签: php rest codeigniter api email

我在Code Igniter中使用REST API,我正在尝试从我的REST控制器发送电子邮件。这就是我尝试过的:

public function registration_post(){

  $jsonArray = json_decode(file_get_contents('php://input'),true);
  $user_name = $jsonArray['user_name'];
  $email = $jsonArray['email'];
  $password = $jsonArray['password'];

    $result = $this->reg_model->insert_api($user_name,$email,$password);
        if ($result){
            $this->load->library('email');
            $from_email = "abc@gmail.com"; 
            $this->email->from($from_email, 'Name'); 
            $this->email->to($email);
            $this->email->subject('email subject');
            $message = 'email body';                 
            $this->email->message($message);
            $this->email->send();
            $data = $this->response($this->reg_model->get($user_name));
        }
} 

这是一个POST请求API,我通过POST调用成功地在数据库中插入用户数据,然后发送电子邮件。但是,电子邮件没有发送。

insert_api()函数将数据插入数据库,我也从get()函数得到响应。

1 个答案:

答案 0 :(得分:0)

编辑设置,通过localhost

中的gmail发送电子邮件
public function registration_post(){

$jsonArray = json_decode(file_get_contents('php://input'),true);
$user_name = $jsonArray['user_name'];
$email = $jsonArray['email'];
$password = $jsonArray['password'];

$result = $this->reg_model->insert_api($user_name,$email,$password);
    if ($result){
        $config = Array(        
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.gmail.com',
            'smtp_port' => 465,
            'smtp_user' => 'YOURGMAIL',
            'smtp_pass' => 'YOURGMAILPASSWORD',
            'smtp_timeout' => '4',
            'mailtype'  => 'html', 
            'charset'   => 'iso-8859-1'
        );
        $this->load->library('email', $config);
        $this->email->set_newline("\r\n"); 
        $from_email = "abc@gmail.com"; 
        $this->email->from($from_email, 'Name'); 
        $this->email->to($email);
        $this->email->subject('email subject');
        $message = 'email body';                 
        $this->email->message($message);
        $this->email->send();
        $data = $this->response($this->reg_model->get($user_name));
    }
}