Codeigniter同时发送单独的电子邮件

时间:2017-06-20 10:03:17

标签: php codeigniter email

使用Codeigniter 3,我有一个Web表单,一旦提交表单就会向站点管理员发送电子邮件 - 这可以按预期工作。

我正在使用电子邮件模板,一旦表单被提交模板1 (发送给网站管理员)。我现在想同时发送模板2 (到提交者的电子邮件地址)。

电子邮件将包含与电子邮件介绍文本相同的内容,以及主题 - 详情如下;

  • 电子邮件模板1 - admin ;

'嗨,网站上已经请求了一个新项目,......'

  • 电子邮件模板2 - 提交者;

'嗨,这是您在网站上请求的项目,...'

我目前的代码如下;

public function sendRequest() {

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

    $from_email     = $this->input->post('email'); 
    $to_email       = 'admin@example.com'; 
    $subject        = 'New Item Request - Admin Copy';
    $name           = $this->input->post('name'); 
    $comment        = $this->input->post('comment');

    $this->email->from($from_email); 
    $this->email->to($to_email);
    $this->email->subject($subject); 

    $data = array(
        'name'          =>  $name,
        'from'          =>  $from_email,
        'comment'       =>  $comment,
        );
    // send email template 1
    $this->email->message($this->load->view('template/email/item_request_template', $data, true));

   // send email template 2 to submitter - how?
   // change $subject to 'New Item Request - User Copy';

    if($this->email->send()) {
        // send the $data to my email template
        $data = array(
         'item_request_name'    => $this->input->post('name'),
         'item_request_email'   => $this->input->post('email'),
         'item_request_comment' => $this->input->post('comment'),
         );
    }
}

有更有效的方法吗?

1 个答案:

答案 0 :(得分:2)

您只需重复首先发送电子邮件所需的所有步骤。唯一不同的是,您需要调用第二个呼叫,并重置所有必需的配置选项。

public function sendRequest() {

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

   $from_email     = $this->input->post('email');
   $to_email       = 'admin@example.com';
   $subject        = 'New Item Request - Admin Copy';
   $name           = $this->input->post('name');
   $comment        = $this->input->post('comment');

   $this->email->from($from_email);
   $this->email->to($to_email);
   $this->email->subject($subject);

   $data = array(
       'name'          =>  $name,
       'from'          =>  $from_email,
       'comment'       =>  $comment,
       );
   // send email template 1
   $this->email->message($this->load->view('template/email/item_request_template', $data, true));

  // send email template 2 to submitter - how?
  // change $subject to 'New Item Request - User Copy';

   if($this->email->send()) {

       $this->email->clear(TRUE); // Pass TRUE as an argument if you are sending attachments

       $this->email->from($from_email); // Update for second email
       $this->email->to($to_email); // Update for second email
       $this->email->subject($subject); // Update for second email

       // send the $data to my email template
       $data = array(
        'item_request_name'    => $this->input->post('name'),
        'item_request_email'   => $this->input->post('email'),
        'item_request_comment' => $this->input->post('comment'),
        );

       $this->email->message($this->load->view('template/email/item_request_template_2', $data, true));

       $this->email->send();
   }
}