简体   繁体   中英

Passing data from one view to email template view on checkbox checked in code-igniter

I'm using a code-igniter to develop an web application. I'm trying to create a dynamic email template which loads data dynamically when I'm check a checkbox from other view.

Here is my scenario: 在此处输入图片说明

If I'm checking a checkbox for HMO_001 , then all data that will loaded in the table for HMO_001 will pass to my email template, and when I'm click on Send button the mail will send to particular landlord.

Now I'm able to send an email to multiple landlords at a time on button click when I'm check multiple checkbox, but the email is hard coded. I'm trying to make it dynamic, but no success.

Here is my Code:

Controller to load table data:

public function index()
    {
        # code...
        $user = $this->ion_auth->user()->row();
        $data['username'] = $user->username;
        $data['user_id'] = $user->id;

        $landlord = $this->input->post('landlord_id');
        $property = $this->input->post('property_id');
        $certificate = $this->input->post('certificate_id');

        if( $landlord =='') {
            $landlord = 0;
        }
        if($property == ''){
            $property = 0;
        }
        if($certificate == ''){
            $certificate = 0;
        }

        $data['landlords'] = $this->c->landlordList();
        //$data['properties'] = $this->c->propertyList();
        $data['certificates'] = $this->c->certificateList();
        //$data['certified'] = $this->c->certificate_List();

        $data['certified'] = $this->c->getcertificateByLandlordProperty($landlord, $property, $certificate);

        // print_r($data['landlords']);
        // print_r($data['properties']);
        // echo "<pre>";
        // print_r($data['certificates']);
        // echo "</pre>";
        // exit();
        $data['title'] = 'Certificate List';

        $this->load->view('template/header', $data);
        $this->load->view('certificate/certificate_list', $data);
        $this->load->view('template/footer');
    }

Controller to send mail:

public function sendMail()
    {
        # code...
        //$data['certified'] = $this->c->getcertificateByLandlordProperty($landlord, $property, $certificate);


        if(isset($_POST['submit'])){//to run PHP script on submit
            if(!empty($_POST['sendMail'])){
            // Loop to store and display values of individual checked checkbox.
                foreach($_POST['sendMail'] as $selected){
                    //echo $selected."</br>";
                }
            }
        }

        $email_to = $this->input->post('sendMail[]');

        <!-- echo "<pre>";
        print_r($email_to);
        echo "</pre>";
        exit(); -->
        $body = $this->load->view('emailTemplate/certTemplate', $data);

        $this->load->library('email');        
        $config = array();
            $config['protocol'] = 'mail';
            $config['smtp_port'] = 587;
            $config['smtp_host'] = 'smtp.office365.com';
            $config['smtp_user'] = 'info@admin.com';
            $config['smtp_pass'] = '**********';
            $config['smtp_crypto'] = 'STARTTLS';   
            $config['newline'] = "\r\n"; //REQUIRED! Notice the double quotes!
            $config['crlf'] = "\r\n";
            $config['mailtype'] = "html";

        $this->email->initialize($config);

        $this->email->from('info@admin.com', 'Administrator');
        $this->email->to($email_to);
        $this->email->subject('Your Subject');
        $this->email->message('$body');

        $sent = $this->email->send();

        if ($sent)
        {
            echo 'OK, mail send successfully';

        } else {
            echo $this->email->print_debugger();
        }
    }

Any kind of help is welcome, Thanks in advance.

To make a customized email message template, you could do like it this :
1. Move each recipient part to foreach loops that you've made prevously, this way you know each of email to recipient that is not being sent.
2. Add a third TRUE parameter on message $body (to return data as a string & not sending it to browser).
3. Add clear() method so each recipient only receive their customized message (to prevent showing all recipients on each recipient).

public function sendMail()
{
    # code...
    //$data['certified'] = $this->c->getcertificateByLandlordProperty($landlord, $property, $certificate);

    if(isset($_POST['submit'])){//to run PHP script on submit
        if(!empty($_POST['sendMail'])){
            $this->load->library('email');        
            $config = array();
            $config['protocol'] = 'mail';
            $config['smtp_port'] = 587;
            $config['smtp_host'] = 'smtp.office365.com';
            $config['smtp_user'] = 'info@admin.com';
            $config['smtp_pass'] = '**********';
            $config['smtp_crypto'] = 'STARTTLS';   
            $config['newline'] = "\r\n"; //REQUIRED! Notice the double quotes!
            $config['crlf'] = "\r\n";
            $config['mailtype'] = "html";

            $this->email->initialize($config);

            // Loop to store and display values of individual checked checkbox.
            foreach($_POST['sendMail'] as $selected){
                $data['email_to'] = $selected;

                $body = $this->load->view('emailTemplate/certTemplate', $data, TRUE); // the TRUE parameter will return data as a string instead of sending it to browser (access recipient email on certTemplate with $email_to variable)

                $this->email->clear();
                $this->email->from('info@admin.com', 'Administrator');
                $this->email->to($email_to);
                $this->email->subject('Your Subject');
                $this->email->message($body); // remove quotes from $body

                $sent = $this->email->send();

                if ($sent)
                {
                    echo 'OK, mail send successfully';

                } else {
                    echo $this->email->print_debugger();
                }
            }
        }
    }

}

Hope it helps..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM