title: Sending Email to multiple addresses from multiselect option using codeigniter-PHP
body: <p>I have a multiselect dropdown, which allows me to select more than one user at a time. I want to be able to select more than one user and when I click on the "Send code" button, the application will snow send email to these users using the email addresses. Currently when I select and click "Send Code", the application will only send email to only one user leaving other once. Please I will like to know to be sending email to all selected users at a time. I'm using CodeIgniter and  below is my code:</p>
  
  <p>Below is the view section of my code, which enable user to select users and send code:</p>
  
  <pre><code> &lt;head&gt;
       &lt;meta charset="UTF-8"&gt;
       &lt;title&gt;Member Invite&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form class="form-horizontal" method="post" action="&lt;?php echo site_url('root/administrator/sendinvite'); ?&gt;"&gt;
          &lt;fieldset&gt;
              &lt;div class="control-group"&gt;
                  &lt;label class="control-label" for="select01"&gt;Select Set&lt;/label&gt;
                  &lt;div class="controls"&gt;
  
                      &lt;select id="select01" class="chzn-select" name="set_code" title="Select the set this student(s) will belong to"&gt;
                          &lt;option value=""&gt;Select set&lt;/option&gt;
                          &lt;?php foreach ($sets as $set) { ?&gt;
                          &lt;option &lt;?php echo set_select('set_code', $set-&gt;group_id); ?&gt; value="&lt;?php echo $set-&gt;group_id; ?&gt;"&gt;&lt;?php echo $set-&gt;group_name; ?&gt;&lt;/option&gt;
                          &lt;?php } ?&gt;
                      &lt;/select&gt;
                      &lt;font size="1" color='red'&gt;&lt;?php echo form_error('set_code'); ?&gt;&lt;/font&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="control-group"&gt;
                  &lt;label class="control-label" for="multiSelect"&gt;Select student(s)&lt;/label&gt;
                  &lt;div class="controls"&gt;
                      &lt;select multiple="multiple" id="multiSelect" name="student_email" class="chzn-select span4" title="Select student(s) for this set"&gt;
                          &lt;option value=""&gt;Select Student&lt;/option&gt;
                          &lt;?php foreach ($students as $student) { ?&gt;
                          &lt;option &lt;?php echo set_select('student_email', $student-&gt;uacc_email); ?&gt; value="&lt;?php echo $student-&gt;uacc_email; ?&gt;"&gt;&lt;?php echo $student-&gt;uacc_username; ?&gt;&lt;/option&gt;
                          &lt;?php } ?&gt;
                      &lt;/select&gt;
                      &lt;p class="help-block"&gt;Start typing to activate auto complete!&lt;/p&gt;
                      &lt;font size="1" color='red'&gt;&lt;?php echo form_error('student_email'); ?&gt;&lt;/font&gt;
                  &lt;/div&gt;
                  &lt;input type="hidden" name="student_id" value="&lt;?php echo $student-&gt;upro_uacc_fk; ?&gt;" class="span6 m-wrap"/&gt;
  
              &lt;/div&gt;
              &lt;div class="form-actions"&gt;
                  &lt;button type="submit" class="btn btn-primary"&gt;Send code&lt;/button&gt;
                  &lt;button type="reset" class="btn"&gt;Cancel&lt;/button&gt;
              &lt;/div&gt;
          &lt;/fieldset&gt;
      &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Below is my Controller, which accepts the selected users and used same to send email:</p>
  
  <pre><code>public function sendinvite() {
      if ($this-&gt;input-&gt;post()) {
  
  
          $this-&gt;form_validation-&gt;set_rules('student_email', 'Student name', 'required');
          $this-&gt;form_validation-&gt;set_rules('set_code', 'Set name', 'required');
  
          if ($this-&gt;form_validation-&gt;run() == FALSE) {
              $this-&gt;data['students'] = $this-&gt;super_model-&gt;get_members_with_out_group();
              $this-&gt;data['sets'] = $this-&gt;super_model-&gt;get_group_list_for_code_invite();
              $this-&gt;data['content'] = 'root/invitestudent';
              $this-&gt;load-&gt;view('layout/root/template', $this-&gt;data);
          } else {
              $config = Array(
                  'protocol' =&gt; 'smtp',
                  'smtp_host' =&gt; 'mail.mydomain.com',
                  'smtp_port' =&gt; 25,
                  'smtp_user' =&gt; 'root', 
                  'smtp_pass' =&gt; '347y6Z45Rwlfub020', 
                  'mailtype' =&gt; 'html',
                  'charset' =&gt; 'iso-8859-1',
                  'wordwrap' =&gt; TRUE
              );
  
              $this-&gt;load-&gt;library('email', $config);
              $len = 8;
              $type = 'HhJjKkMmNnPpQqRrSsTt';
              $set = $this-&gt;input-&gt;post('set_code');
              //$new_password = rand(34546, 89757);
              $this-&gt;email-&gt;from('admin@passwordmanager.com', "Admin Manager");
              $this-&gt;email-&gt;to($this-&gt;input-&gt;post('student_email'));
              //$this-&gt;email-&gt;cc($this-&gt;input-&gt;post('student_email'));
              $this-&gt;email-&gt;subject('BSN-Set Code');
              //$this-&gt;flexi_auth-&gt;forgotten_password($identity);
              $this-&gt;email-&gt;message("You are invited to join the group. Your set code is: $set");
  
              $data['message'] = "Sorry Unable to send email...";
              if ($this-&gt;email-&gt;send()) {
                  $data['message'] = "Mail sent...";
              }
              redirect('root/administrator/invitesuccess');
          }
      } else {
          $this-&gt;data['students'] = $this-&gt;super_model-&gt;get_members_with_out_group();
          $this-&gt;data['sets'] = $this-&gt;super_model-&gt;get_group_list_for_code_invite();
          $this-&gt;data['content'] = 'root/invitestudent';
          $this-&gt;load-&gt;view('layout/root/template', $this-&gt;data);
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/YaZTn.png" rel="nofollow noreferrer">Image is here</a></p>