dongpu1879 2018-01-02 04:58
浏览 41
已采纳

将数据从一个视图传递到代码点火器中选中的复选框上的电子邮件模板视图

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: enter image description here

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'] = "
"; //REQUIRED! Notice the double quotes!
            $config['crlf'] = "
";
            $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.

  • 写回答

1条回答 默认 最新

  • douban2014 2018-01-03 03:30
    关注

    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'] = "
    "; //REQUIRED! Notice the double quotes!
                $config['crlf'] = "
    ";
                $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..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!