响应电子邮件不会被发送

时间:2016-04-26 07:10:24

标签: codeigniter

我试图使用codeigniter进行自动回复。

     public function response() 
    {
    $status             = $this->uri->segment(3);           
    $id                 = $this->uri->segment(4);
    $id_user            = $this->uri->segment(5);

    $data['id']         = $id;
    $data['status']     = $status ;

   $this->messages_model->pending($data); <<<<update database

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

   $config['protocol']  = 'smtp';           
   $config['smtp_host']    = 'host.org';            
   $config['smtp_port'] = 'secretport';         
   $config['smtp_user'] = 'from_email@example.com';          
   $config['smtp_pass'] = 'examplepassword';        
   $config['smtp_timeout']  = '100';        
   $config['mailtype']      = 'html';         
   $config['charset']       = 'utf-8';              
   $config['newline']       = "\r\n";       

    if($status == "approved") {
    $message='your form is approved';
    redirect("/response/approved");
           }else{
    $message='your form is approved';
    redirect("/response/rejected");
       }         
         $to_email = $this->messages_model->reply($id_user);

         $this->email->initialize($config);
         $this->email->from('blablabla@email.com', 'NAME');
         $this->email->to($to_email);
         $this->email->subject('Confirmation Note'); 
         $this->email->message($message);
     }

它已成功更新数据库,但我的收件箱中没有电子邮件。

此代码有问题吗?

2 个答案:

答案 0 :(得分:2)

您需要在电子邮件部分之后放置重定向的代码。

     $to_email = $this->messages_model->reply($id_user);

         $this->email->initialize($config);
         $this->email->from('blablabla@email.com', 'NAME');
         $this->email->to($to_email);
         $this->email->subject('Confirmation Note'); 
         $this->email->message($message);
         $this->email->send();
         if($status == "approved") {
              $message='your form is approved';
              redirect("/response/approved");
         }else{
              $message='your form is approved';
              redirect("/response/rejected");
         } 

答案 1 :(得分:1)

如重定向功能的CI文档中所述:

  

此函数将终止脚本执行

参考:Redirect function

首先,将重定向移动到函数的末尾。

其次,在您设置所有电子邮件属性后,用户$ this-&gt; email-&gt; send()实际发送电子邮件。

参考:Email library

第三,拒绝信息是错误的:)

评论中更新问题:

 $to_email = $this->messages_model->reply($id_user);
 $this->email->initialize($config);
 $this->email->from('blablabla@email.com', 'NAME');
 $this->email->to($to_email);
 $this->email->subject('Confirmation Note'); 


if ($status == "approved") {
    $message='your form is approved';
    $this->email->message($message);
    $this->email->send();
    redirect("/response/approved");
} else {
    $message='your form is rejected';
    $this->email->message($message);
    $this->email->send();
    redirect("/response/rejected");
}