Php Codeigniter同时发送不同的电子邮件

Php Codeigniter同时发送不同的电子邮件,php,codeigniter,email,Php,Codeigniter,Email,使用Codeigniter 3,我有一个web表单,在表单提交后会向站点管理员发送一封电子邮件-这与预期一样有效 我正在使用电子邮件模板,一旦表单提交,模板1将被发送(给网站管理员)。我现在想同时发送模板2(到提交者的电子邮件地址) 电子邮件将包含相同的内容,除了电子邮件介绍文本和主题详细信息如下 电子邮件模板1-发送至管理员 '您好,网站上请求了一个新项目,…' 电子邮件模板2-发送至提交人 “您好,这是您在网站上请求的项目,…” 我目前的代码如下 public function se

使用Codeigniter 3,我有一个web表单,在表单提交后会向站点管理员发送一封电子邮件-这与预期一样有效

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

电子邮件将包含相同的内容,除了电子邮件介绍文本和主题详细信息如下

  • 电子邮件模板1-发送至管理员
'您好,网站上请求了一个新项目,…'

  • 电子邮件模板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'),
         );
    }
}

有没有更有效的方法呢?

您只需重复发送电子邮件所需的所有步骤即可。唯一的区别是,对于第二次调用,您需要调用并重置所有必需的配置选项

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

由于您有两个不同的收件人,内容不同,因此您必须单独发送邮件—这是唯一想到的—也许您可以尝试创建一个自己的邮件控制器来处理整个邮件发送过程