遇到有关如何使用Codeigniter在电子邮件中附加文件的麻烦

时间:2019-01-08 06:41:40

标签: codeigniter codeigniter-3 codeigniter-2

这是我的视图表格。用户将在此处发送带有附件的文件并通过电子邮件发送电子邮件。(这是我的查看表单的快捷方式。问题是附件)。

<form action="<?php echo base_url(); ?>hr_mailer/create" method="POST">
                    <table class="table" style="margin-left: 15px; text-align: left; width: 870px;">
            <tr>
    <td style="font-weight: bold;">Attachment/s: </td>
    <td colspan="4"><input type="file" name="upload" id="file" /><label for="file">Choose a file</label>
                        </tr>
                        <tr>
                            <td colspan="4"><input type="submit" name="submit" value="Send Email" class="button1" style="float: right;" onclick="return confirm('You are about send the email, are you sure?')"> 
                                <button type="reset" value="Reset" class="button1" style="float: right; margin-right: 5px;">Clear</button></td>
                        </tr>
                    </table>            
                </form>

此功能将捕获表单操作

public function create() {
            if ($this->input->post('submit')) {

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

                  $config['upload_path'] = './uploads/';
                  $config['allowed_types'] = 'gif|jpg|png';
                  $config['max_size'] = '100000';
                  $config['max_width']  = '1024';
                  $config['max_height']  = '768';

                  $this->load->library('upload', $config);
                  $this->upload->do_upload('upload');
                  $upload_data = $this->upload->data();



                  $form = array(
                    'sender' => $this->input->post('hr'),
                    'date' => $this->input->post('email_date')
                );


                $to = $this->input->post('to');
                $cc = $this->input->post('cc');
                $bcc = $this->input->post('bcc');
                $message = $this->input->post('message');
                $subj = $this->input->post('subject');            


                $this->notify($to, $cc, $bcc, $message, $subj,$upload_data );
                $this->mailer->insert($form);                  

                 $this->session->set_flashdata('msg', 'Email Sent!');
                    redirect(base_url().'hr_mailer/');


            } else {
                $data['uid'] = $this->user->get_uid();
                $content = $this->load->view("hr_mailer/create", $data, true);
                $this->render("", "", "", $content);
            }
        }

这是我控制器中的另一个功能。

private function notify($to, $cc, $bcc, $message, $subj,$upload_data) {
        $this->user = new Employee($this->session->userdata('username'));
        $var = "cebuhr@1and1.com";
        $name = "HR Announcement";

//        $config['charset'] = 'iso-8859-1';
//        $config['wordwrap'] = FALSE;
//        $config['mailtype'] = 'html';

        $this->load->library('email');
//        $this->email->initialize($config);

        //for testing purposes only. Must not be pushed to production
         $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => 465,
            'smtp_user' => 'email',
            'smtp_pass' => 'password',
            'mailtype' => 'html',
            'charset' => 'utf-8'
        );  

        $this->email->initialize($config);
        $this->email->set_mailtype("html");
        $this->email->set_newline("\r\n");

        $this->email->from($var, $name);
        $this->email->to($to);
        $this->email->cc($cc);
        $this->email->bcc($bcc);

        $this->email->subject($subj);
        $this->email->message($message);
        $this->email->attach($upload_data['full_path']);

        if($this->email->send()){
            echo 'SUCCESS';
        } else {
            show_error($this->email->print_debugger());
        }
         return true;
    }

我对CI来说还很陌生,有些麻烦。我来自其他框架。在我的研究中,放在attach()部分的变量是上载文件的完整路径名,这是我很难找到的地方。

1 个答案:

答案 0 :(得分:0)

在您的函数中创建...

public function create() {
            if ($this->input->post('submit')) {

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

                  $config['upload_path'] = './uploads/';
                  $config['allowed_types'] = 'gif|jpg|png';
                  $config['max_size'] = '100000';
                  $config['max_width']  = '1024';
                  $config['max_height']  = '768';

                  $this->load->library('upload', $config);
                  $this->upload->do_upload('upload');
                  $upload_data = $this->upload->data();
                  $file_path = $config['upload_path'].$upload_data[file_name]; // get path


                  $form = array(
                    'sender' => $this->input->post('hr'),
                    'date' => $this->input->post('email_date')
                );


                $to = $this->input->post('to');
                $cc = $this->input->post('cc');
                $bcc = $this->input->post('bcc');
                $message = $this->input->post('message');
                $subj = $this->input->post('subject');            


                // $this->notify($to, $cc, $bcc, $message, $subj,$upload_data );
                $this->notify($to, $cc, $bcc, $message, $subj,$file_path );
                // pass the generated path.. 
                $this->mailer->insert($form);                  

                 $this->session->set_flashdata('msg', 'Email Sent!');
                    redirect(base_url().'hr_mailer/');


            } else {
                $data['uid'] = $this->user->get_uid();
                $content = $this->load->view("hr_mailer/create", $data, true);
                $this->render("", "", "", $content);
            }
        }

然后更改最后一个参数并将var附加到notify函数中

private function notify($to, $cc, $bcc, $message, $subj,$file_path) {
        $this->user = new Employee($this->session->userdata('username'));
        $var = "cebuhr@1and1.com";
        $name = "HR Announcement";

//        $config['charset'] = 'iso-8859-1';
//        $config['wordwrap'] = FALSE;
//        $config['mailtype'] = 'html';

        $this->load->library('email');
//        $this->email->initialize($config);

        //for testing purposes only. Must not be pushed to production
         $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => 465,
            'smtp_user' => 'email',
            'smtp_pass' => 'password',
            'mailtype' => 'html',
            'charset' => 'utf-8'
        );  

        $this->email->initialize($config);
        $this->email->set_mailtype("html");
        $this->email->set_newline("\r\n");

        $this->email->from($var, $name);
        $this->email->to($to);
        $this->email->cc($cc);
        $this->email->bcc($bcc);

        $this->email->subject($subj);
        $this->email->message($message);
        $this->email->attach($file_path);

        if($this->email->send()){
            echo 'SUCCESS';
        } else {
            echo "<pre>";
            print_r($this->email->print_debugger());
            EXIT;
        }
         return true;
    }

以此替换您的代码... 您没有正确捕获路径,并且没有在任何变量中捕获调试器..它在打印之前就熄灭了..在创建函数时...您的位置得到了重定向..您没有任何通知函数的条件..