【问题标题】:Codeigniter Send Separate Emails SimultaneouslyCodeigniter 同时发送单独的电子邮件
【发布时间】:2017-06-20 10:03:16
【问题描述】:

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

我正在使用电子邮件模板,一旦提交表单模板 1 就会发送(发送给站点管理员)。我现在想同时发送 template 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'),
         );
    }
}

有没有更有效的方法?

【问题讨论】:

  • 因为你有 2 个不同的收件人,不同的内容,你必须分开发送 - 唯一想到的 - 也许你可以尝试制作一个自己的邮件控制器来处理整个邮件发送过程

标签: php codeigniter email


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-01-07
    • 2019-08-07
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2012-12-16
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多