Sending Email to multiple addresses from multiselect option using codeigniter-PHP

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:

Below is the view section of my code, which enable user to select users and send code:

<head>
     <meta charset="UTF-8">
     <title>Member Invite</title>
</head>
<body>
    <form class="form-horizontal" method="post" action="<?php echo site_url('root/administrator/sendinvite'); ?>">
        <fieldset>
            <div class="control-group">
                <label class="control-label" for="select01">Select Set</label>
                <div class="controls">

                    <select id="select01" class="chzn-select" name="set_code" title="Select the set this student(s) will belong to">
                        <option value="">Select set</option>
                        <?php foreach ($sets as $set) { ?>
                        <option <?php echo set_select('set_code', $set->group_id); ?> value="<?php echo $set->group_id; ?>"><?php echo $set->group_name; ?></option>
                        <?php } ?>
                    </select>
                    <font size="1" color='red'><?php echo form_error('set_code'); ?></font>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="multiSelect">Select student(s)</label>
                <div class="controls">
                    <select multiple="multiple" id="multiSelect" name="student_email" class="chzn-select span4" title="Select student(s) for this set">
                        <option value="">Select Student</option>
                        <?php foreach ($students as $student) { ?>
                        <option <?php echo set_select('student_email', $student->uacc_email); ?> value="<?php echo $student->uacc_email; ?>"><?php echo $student->uacc_username; ?></option>
                        <?php } ?>
                    </select>
                    <p class="help-block">Start typing to activate auto complete!</p>
                    <font size="1" color='red'><?php echo form_error('student_email'); ?></font>
                </div>
                <input type="hidden" name="student_id" value="<?php echo $student->upro_uacc_fk; ?>" class="span6 m-wrap"/>

            </div>
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">Send code</button>
                <button type="reset" class="btn">Cancel</button>
            </div>
        </fieldset>
    </form>
    </body>
</html>

Below is my Controller, which accepts the selected users and used same to send email:

public function sendinvite() {
    if ($this->input->post()) {


        $this->form_validation->set_rules('student_email', 'Student name', 'required');
        $this->form_validation->set_rules('set_code', 'Set name', 'required');

        if ($this->form_validation->run() == FALSE) {
            $this->data['students'] = $this->super_model->get_members_with_out_group();
            $this->data['sets'] = $this->super_model->get_group_list_for_code_invite();
            $this->data['content'] = 'root/invitestudent';
            $this->load->view('layout/root/template', $this->data);
        } else {
            $config = Array(
                'protocol' => 'smtp',
                'smtp_host' => 'mail.mydomain.com',
                'smtp_port' => 25,
                'smtp_user' => 'root', 
                'smtp_pass' => '347y6Z45Rwlfub020', 
                'mailtype' => 'html',
                'charset' => 'iso-8859-1',
                'wordwrap' => TRUE
            );

            $this->load->library('email', $config);
            $len = 8;
            $type = 'HhJjKkMmNnPpQqRrSsTt';
            $set = $this->input->post('set_code');
            //$new_password = rand(34546, 89757);
            $this->email->from('admin@passwordmanager.com', "Admin Manager");
            $this->email->to($this->input->post('student_email'));
            //$this->email->cc($this->input->post('student_email'));
            $this->email->subject('BSN-Set Code');
            //$this->flexi_auth->forgotten_password($identity);
            $this->email->message("You are invited to join the group. Your set code is: $set");

            $data['message'] = "Sorry Unable to send email...";
            if ($this->email->send()) {
                $data['message'] = "Mail sent...";
            }
            redirect('root/administrator/invitesuccess');
        }
    } else {
        $this->data['students'] = $this->super_model->get_members_with_out_group();
        $this->data['sets'] = $this->super_model->get_group_list_for_code_invite();
        $this->data['content'] = 'root/invitestudent';
        $this->load->view('layout/root/template', $this->data);
    }
}

Screenshot:

Thanks guys in anticipation

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.