Welcome Guest, Not a member yet? Register   Sign In
Send Separate Emails Simultaneously
#1

Hi,

First post on here - hopefully someone can help Smile

Using Codeigniter 3, I have a web form that sends an email to the site admin once a form is submitted - this works as expected.

I am using email templates, once the form is submitted template 1 is sent (to the site admin). I would now like to simultaneously send template 2 (to the submitter's email address).

The emails will contain the same content apart from the email intro text, and subject - details below;

Email Template 1 - to admin;
'Hi, a new item has been requested on the site, ...'

Email Template 2 - to submitter;
'Hi, Here is the item you have requested on the site, ...'

My current code is as follows;

Code:
public function sendRequest() {

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

   $from_email     = $this->input->post('email');
   $to_email       = '[email protected]';
   $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'),
        );
   }
}

Not sure what I need to add in order to send the second email template?

Any help would be appreciated.
Reply
#2

Before sending the second email you need to clear the email library. Otherwise it will reuse the provided settings from the first call

From the documentation
Quote:Initializes all the email variables to an empty state. This method is intended for use if you run the email sending method in a loop, permitting the data to be reset between cycles

CI_Email::clear
Reply
#3

Hi Martin,

Thanks for the suggestion, I've already tried that and still only the first email is sending. Perhaps i'm not adding it in the correct place, if you could show an example of how it should look that may help.

Thanks
Reply
#4

You could have something like this

PHP Code:
public function sendRequest() {

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

 
  $from_email     $this->input->post('email');
 
  $to_email       '[email protected]';
 
  $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'$datatrue));

 
 // 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'$datatrue));

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


You simply have to repeat all the steps required for sending an email in the first place. Only difference is that for the second call you need to call
PHP Code:
$this->email->clear(); 

And reset all the required config options

Hope this helps you on your way Smile
Reply
#5

Perfect! Thank you Smile

I wasn't doing this part;

Code:
       $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
Reply
#6

You are welcome Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB