【问题标题】:How to send mail from rest API in code igniter rest controller?如何在代码点火器休息控制器中从休息 API 发送邮件?
【发布时间】:2017-01-18 16:09:45
【问题描述】:

我在 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() 函数得到响应。

【问题讨论】:

标签: php rest codeigniter api email


【解决方案1】:

编辑在本地主机中通过 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));
    }
} 

【讨论】:

  • 我怎么知道我的 smtp_host?
  • 兄弟我不明白什么是协议=>sendmail 的意思?
  • 我已经更新了答案。通过codeigniter documentation您将获得协议的详细信息
  • 刚刚编辑了一个电子邮件配置在我的本地主机中工作。
猜你喜欢
  • 2019-09-07
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多