简体   繁体   中英

How to send mail from rest API in code igniter rest controller?

I am using REST API in Code Igniter and I am trying to send an email from my REST controller. This is what I've tried:

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));
        }
} 

This is a POST request API in which I am successfully inserting user data in the database via a POST call and then sending an email. However, the email is not sending.

insert_api() function inserts the data into database and I get response from get() function also.

Edit setting for sending email through gmail in localhost

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));
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM