Werner Pelser
Werner Pelser

Reputation: 63

Attaching Multiple files/Images to my phpmailer function in codeigniter

Good day.

I have an form witch displays all the information from my DB with the id the user selected, i then have a button that sends the data with phpmailer to the selected users email. In this information i have images that is uploaded to my server and the file path to my DB.

What i can not figure out is how do i get the images/files to be sent with the email when the user hits the forward call button.

Please if you can guide me in the right direction as most of the post i have read is for single files/images and adding it straight from the directory.

My View View

Folder with Images [Folder with Images[2]

DB Images Image of DB

    My View

    '''<div class="col-lg-12">
                    <ul class="gallery">
                        <?php foreach($files as $row):
                            $images = explode(',', $row->files);
                                foreach($images as $files):?>
                                    <img src="<?= base_url() . "uploads/ob_images/$files"; ?>" width="250" height="250">
                            <?php endforeach; ?>
                        <?php endforeach; ?>
                    </ul>
                </div>
        </div>'''

My Controller
'''function forward_call($id){

            $data = array(

                'formTitle' => 'Forward Call',
                'title' => 'Call Management'
            );

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

            $this->form_validation->set_rules('call_attended_by', 'Call Attendad By', 'trim|required|callback_reason_for_update');

            if ($this->form_validation->run() === FALSE)
            {
                $data['supplier'] = $this->admin_model->get_supplier_list();
                $data['priority'] = $this->admin_model->get_priority_level();
                $data['camera'] = $this->admin_model->get_camera();
                $data["data"] = $this->admin_model->get_current_call('', '', $id);
                $results = $this->admin_model->get_supplier_list();
                $data['files'] = $this->admin_model->get_call_images($id);
                $this->load->view('frame/header_view');
                $this->load->view('frame/sidebar_nav_view');
                $this->load->view('helpdesk/forward_call', $data);         
            }
            else
            {

                $message = "Your assistants is needed with the following call.<br><br>

                <ins>Call ID:</ins> ".$_POST['id'].";<br><br>

                <ins>Date Call Was Logged At HTCC:</ins> ".$_POST['date_logged']."<br><br>

                <ins>Date Acknowledged By Technician:</ins> ".$_POST['date_logged']."<br><br>

                <ins>Technician:</ins> ".$_POST['call_attended_by']."<br><br>

                <ins>Call Status:</ins> ".$_POST['call_status']."<br><br>

                <ins>Call Status:</ins> ".$_POST['location']."<br><br>

                <ins>Call Priority:</ins> ".$_POST['priority_level']."<br><br>

                <ins>Type Of Equipment:</ins> ".$_POST['type_of_equipment']."<br><br>

                <ins>Call Description:</ins> ".$_POST['call_description']."<br><br>

                <ins>Fault Found:</ins> ".$_POST['fault_found']."<br><br>

                If you need any futher assists please contact Helpdesk or ".$_POST['call_attended_by']."<br><br>

                Thanks HTCC Helpdesk Team!!";

                $subject = "HTCC Call Assistants";
                    $this->admin_model->send_call($message,$subject,$_POST['email']);
                    $this->admin_model->forwarded_call($id);
                redirect( base_url('admin/open_call_list'));

            }'''

forward call
'''function forwarded_call($id = 0)
    {
        //$this->load->helper('url');


        $data = array(
            'id' => $this->input->post('id'),
            'call_attended_by' => $this->input->post('call_attended_by'),
            'date_acknowledged_by_tech' => $this->input->post('date_acknowledged_by_tech'),
            'priority' => $this->input->post('priority_level'),
            'supplier' => $this->input->post('supplier1'),
            'forwarded_to' => $this->input->post('supplier1'),
            'technician_email' => $this->input->post('technician_email'),
            'date_forwarded' => date("Y-m-d H:i:s"),
        );

                $message = "The call with ID".$_POST['id']." you have logged will be forwarded to ".$_POST['supplier1']." .<br><br>

                If you need any futher assists please contact ".$_POST['call_attended_by']." or Helpdesk;<br><br>

                Thanks HTCC Helpdesk Team!!";

                $subject = "HTCC Helpdesk";
                $this->admin_model->send_call($message,$subject,$_POST['user_email']);

        $module = "Helpdesk";
            $activity = "Call Forwarded".$data['supplier'];
            $this->insert_log($activity, $module);

        if ($id == 0) {
            return $this->db->insert('tbl_calls', $data);
        } else {
            $this->db->where('id', $id);
            return $this->db->update('tbl_calls', $data);
        }

    }'''

My Phpmailer

'''function send_call($message,$subject,$sendTo){
        require_once APPPATH.'libraries/mailer/class.phpmailer.php';
        require_once APPPATH.'libraries/mailer/class.smtp.php';
        require_once APPPATH.'libraries/mailer/mailer_config.php';
        include APPPATH.'libraries/mailer/template/template.php';

        $mail = new PHPMailer(true);
        $mail->IsSMTP();
        try
        {
            $mail->SMTPDebug = 1;  
            $mail->SMTPAuth = true; 
            $mail->SMTPSecure = 'ssl'; 
            $mail->Host = HOST;
            $mail->Port = PORT;  
            $mail->Username = GUSER;  
            $mail->Password = GPWD;     
            $mail->SetFrom(GUSER, 'Helpdesk');
            $mail->Subject = "DO NOT REPLY - ".$subject;
            $mail->IsHTML(true);   
            $mail->WordWrap = 0;


            $hello = '<h1 style="color:#333;font-family:Helvetica,Arial,sans-serif;font-weight:300;padding:0;margin:10px 0 25px;text-align:center;line-height:1;word-break:normal;font-size:38px;letter-spacing:-1px">Hello Team, &#9786;</h1>';
            $thanks = "<br><br><i>This is autogenerated email please do not reply.</i><br/><br/>Thanks,<br/>Helpdesk<br/><br/>";

            $body = $hello.$message.$thanks;
            $mail->Body = $header.$body.$footer;
            $mail->AddAddress($sendTo);

            if(!$mail->Send()) {
                $error = 'Mail error: '.$mail->ErrorInfo;
                return array('status' => false, 'message' => $error);
            } else { 
                return array('status' => true, 'message' => '');
            }
        }
        catch (phpmailerException $e)
        {
            $error = 'Mail error: '.$e->errorMessage();
            return array('status' => false, 'message' => $error);
        }
        catch (Exception $e)
        {
            $error = 'Mail error: '.$e->getMessage();
            return array('status' => false, 'message' => $error);
        }

    }'''

Upvotes: 0

Views: 454

Answers (1)

Synchro
Synchro

Reputation: 37810

You have not posted the code for what you have tried so far, but I will have a guess at what you need.

I'm assuming that you have some basic mail script already, perhaps based on the examples provided with PHPMailer like this one.

It looks like your DB contains multiple filenames in one field, so you will need to split those apart before adding them as attachments. I'm assuming that you have done some DB query and the current row of data is in $row.

$imageFolder = '/path/to/image/folder/';
$files = explode(',', $row['files']);
foreach ($files as $file) {
    $mail->addAttachment($imageFolder . $file);
}

That's all that's needed to add the files as simple attachments.

Upvotes: 2

Related Questions