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..

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料