CodeIgniter Forums
Send Separate Emails Simultaneously - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Send Separate Emails Simultaneously (/thread-68287.html)



Send Separate Emails Simultaneously - johnny_s - 06-20-2017

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.


RE: Send Separate Emails Simultaneously - Martin7483 - 06-20-2017

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


RE: Send Separate Emails Simultaneously - johnny_s - 06-20-2017

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


RE: Send Separate Emails Simultaneously - Martin7483 - 06-20-2017

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


RE: Send Separate Emails Simultaneously - johnny_s - 06-20-2017

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



RE: Send Separate Emails Simultaneously - Martin7483 - 06-20-2017

You are welcome Smile