Возникли проблемы с прикреплением файла в письме с использованием codeigniter - PullRequest
0 голосов
/ 08 января 2019

Это моя форма просмотра. Здесь пользователь отправит и отправит электронное письмо с прикрепленным файлом / файлами. (Это ярлык для моей формы просмотра. Проблема в прикрепленном файле.)

<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 голосов
/ 08 января 2019

в вашей функции создать ...

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);
            }
        }

, а затем измените последний параметр и добавьте переменную в функцию уведомления

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;
    }

замени свой код этим ... вы не фиксируете путь должным образом, и вы не захватили отладчик ни в одной переменной ... он вышел до того, как напечатает ... при создании функции ... ваше местоположение перенаправляется .. у вас нет условий для функции уведомления ..

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...