cakePHP电子邮件附件

时间:2013-08-11 10:22:10

标签: file email cakephp

我在我的控制器中使用此功能,用于向我正在构建的站点中的作业的广告商发送电子邮件;该函数可能看起来有点过载但它基本上做的是:

  1. 获取申请某项工作的人的详细信息,
  2. 工作细节,
  3. 简历
  4. 所有这些信息捆绑在一起,并在成功保存发送电子邮件后发送给模型

        public function sendapplication() {
    
        if ($this -> request -> is('post')) {
            $this -> JobApplication -> create();
            $userid = CakeSession::read("UserAuth.User.id");
            //this reads the current user id in the session established when the user logged in and uses it to store the job alert
            $this -> JobApplication -> data['JobApplication']['user_id'] = $userid;
    
            //$this -> JobApplication -> data['JobApplication']['job_post_id'] = $this -> JobApplication -> JobPost -> id;
            $job_post_id = CakeSession::read('jobpost.id');//reading the job id session set above
            $this -> JobApplication -> data['JobApplication']['job_post_id'] = $job_post_id;
    
            $minorjobdetails = $this -> JobApplication -> sendapplicationmodel($job_post_id);
    
            foreach ($minorjobdetails as $minorjobdetail) :
                $this -> JobApplication -> data['JobApplication']['jobTitle'] = $minorjobdetail['JobPost']['jobTitle'];
                $this -> JobApplication -> data['JobApplication']['employer'] = $minorjobdetail['JobPost']['contactName'];
            endforeach;
    
            $jobless = $this->JobApplication->JobPost->findById($job_post_id );
            $job_applicant = $this->JobApplication->User->findById($userid);
    
            $job_resume = $this -> request-> data['JobApplication']['resume_id'];
            $attachedresume = current($this -> request-> data['JobApplication']['quickresume']);
            $attachedresumepath = next($this -> request-> data['JobApplication']['quickresume']);
            $attachedresumepath1 = next($this -> request-> data['JobApplication']['quickresume']);
            $this -> log('Job resume id-' . $job_resume, LOG_DEBUG);
            if ($this -> JobApplication -> save($this -> request -> data, false)) {
                //$this->JobApplication->sendJobApplicationMail();
                //$this -> Session -> setFlash(__('job application sent'));
                $this->Session->setFlash('job application sent successfully', 'success', array('class' => 'alert alert-success'));
                $this->JobApplication->sendJobApplicationMail($jobless, $job_applicant, $job_resume, $attachedresume, $attachedresumepath, $attachedresumepath1);
                $this->JobApplication->deliveryReportEmail($job_applicant, $jobless);
                $this -> redirect('/jobsposted');
            } else {
                $this -> Session -> setFlash(__('job application could not be sent. Please, try again.'));
            }
    
        }
    
    }
    

    简历有两种情况;用户可能在他/她申请工作之前已经上传到服务器或正在附加服务器。我的问题是在表单上进行附件时出现的。这是我的表格:

        <?php 
    
            echo $this->Form->create('JobApplication', array('type' => 'file', 'enctype' =>'multipart/form-data', 'controller' => 'JobApplications', 'action' => 'sendapplication'));
    
    
            foreach($jobseeker as $js):
            echo $this->Form->input('fullname', array('class' => 'span6', 'value' => $js['User']['first_name']));
            // debug($js['User']['first_name']);
    
            echo $this->Form->input('lastname', array('class' => 'span6', 'value' => $js['User']['last_name'] ));
            echo "<div class=\"input-prepend\">";
            echo $this->Form->label('email');
            echo "<span class=\"add-on\"><i class=\" icon-envelope\"></i></span>".$this->Form->input('email', array('class' => 'span6', 'value' => $js['User']['email'], 'label' => false, 'div' => false));
            echo "</div>";
            endforeach;
            echo $this->Form->input('comment_application_letter', 
                                        array(
                                                'type' => 'textarea',
                                                'rows'=>10,
                                                'cols' => 50,
                                                'class' => 'span6', 
                                                'label' => 'Comment/ Application letter', 
                                                'placeholder' => 'E.g. My name is John Doe. I would like to apply for the marketing job position as posted on ABC company site on mm/dd/yyyy'
                                        )
                                    );
    
    
            echo $this->Form->input('JobApplication.resume_id', array('label' => 'Select resume', 'type' => 'select', 'options' => $myResume));
            // echo $this->Form->label($fieldName = 'selected-file', $text = 'Resume:');
    
            // echo $this->Form->input('JobApplication.dropboxurl',array('type'=>'dropbox-chooser', 'name'=>'selected-file', 'id'=>'db-chooser', 'label' => false, 'visibilty' => 'hidden'));
            echo $this->Form->input('JobApplication.quickresume', array('type' => 'file', 'label' => 'Attach resume'));
            // echo $this->Form->input('', array('label' => 'Resume url', 'id' => 'dropboxurl', 'type' => 'text', 'div' => false, 'disabled', 'value' => '', 'class' => 'span6'));
    
            echo "<br>";
    
            echo $this->Form->button(__('Submit'),array('type' => 'submit', 'class' => 'btn btn-primary'))
    
        ?>
    

    从表单中我想获得已附加的简历并将其与用户的详细信息一起发送; 以下是模型中负责发送电子邮件的功能

    public function sendJobApplicationMail($jobless, $job_applicant, $job_resume, $attachedresume, $attachedresumepath, $attachedresumepath1) {
        // send email to newly created user
        $userId = $jobless['JobPost']['id'];
        $email = new CakeEmail();
    
        $fromConfig = 'example@example.com';
        $fromNameConfig = 'Jobs';
        $email -> from(array($fromConfig => $fromNameConfig));
        $email -> sender(array($fromConfig => $fromNameConfig));
        $email -> to($jobless['JobPost']['jobapplicationEmail']);
        $email -> subject(SITE_NAME . ': ' . __('Job Application'));
    
        if(!empty($attachedresume)){
            //file 
            pr($attachedresume);
            pr($attachedresumepath);
            pr($attachedresumepath1);
    
        // pr($email->filePaths = array($attachedresumepath1)); 
        // exit;
        $email -> attachments(array('CV'=> $attachedresume));
    //exit();
        }
    
    
    
        $email -> emailFormat('html');
        $email -> template('applicant', 'fancy');
        $email -> theme('Default');
        $email -> viewVars(array('employer' => $jobless['JobPost']['contactName'], 'applicant_fname' => $job_applicant['User']['first_name'], 'applicant_sname' => $job_applicant['User']['last_name'], 'aplicant_email' => $job_applicant['User']['email']));
    
        // $employer_contact_name = $jobless['JobPost']['contactName'];
        // $job_applicant_fname = $job_applicant['User']['first_name'];
    
        //$email->transport('Debug');
        $body = __('Hi %s,<br/><br/>The below named person has shown interest on a job you posted;the details of the applicant are as follows: %s.<br/><br/>Thanks,<br/>%s', $jobless['JobPost']['contactName'], $job_applicant['User']['first_name'], SITE_NAME);
        try {
            $result = $email -> send($body);
            $this -> log('Job apllication mail sent to job post id-' . $userId, LOG_DEBUG);
        } catch (Exception $ex) {
            // we could not send the email, ignore it
            $result = "Could not send job application  email to userid-" . $userId;
            $this -> log($result, LOG_DEBUG);
        }
    }
    

    我一直在尝试调试,这是我在尝试提交表单时得到的结果;

    找不到档案:“”

    错误:发生了内部错误。

    堆栈跟踪 APP / Model / JobApplication.php第98行→CakeEmail-&gt;附件(数组) 数组('CV'=&gt;'CAT 1 EvnCourseMay13.doc')

    任何帮助将不胜感激。 感谢

1 个答案:

答案 0 :(得分:0)

异常消息非常清楚:无法找到该文件。调试代码(获胜的单元测试)并找出传递的文件路径值为空或错误的文件路径的原因。

我想说问题是你错过了传递整个路径,你只需要使用文件名。读取堆栈跟踪。

顺便说一句:如果你不想让这些文件可以公开访问,你就不应该把它们放到webroot中...而且你存储文件的方式存在很多其他问题,但那是另一个故事。