如何在CakePHP 2.0中发送带附件的电子邮件?

时间:2012-04-06 15:37:56

标签: cakephp cakephp-2.0

我正在尝试使用CakePHP 2.0发送带附件的电子邮件。

该文件由用户通过表格提交。

到目前为止,我有:

App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->attachments = array($this->data['Opportunity']['resume_file']['tmp_name']);
$email->viewVars(array('name' => $this->data['Opportunity']['name']));
$email->template('application')
    ->emailFormat('html')
    ->to(TEST_CONTACT)
    ->from(EMAIL_CONTACT)
    ->subject('New application received')
    ->send();

电子邮件发送并且看起来很好,但没有附件。

我做错了什么?

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

由于某些原因,除非你声明,否则CakePHP不会做附件 首先是filePaths。

我遇到了同样的问题而我找不到很多答案 这个问题。我花了一段时间才解决问题,澄清一下,

我的工作
$this->Email->filePaths  = array('/home/username/');
       $this->Email->attachments =array('article_button.png');
$this->Email->to      = 'em...@email.co.uk';
    $this->Email->subject = 'Something';
    $this->Email->replyTo = $client['Client']['email'];
    $this->Email->from    = $client['Client']['email'];
    $this->Email->sendAs  = 'html';

    if($this->Email->send('Testing', null, null)){
      die('Email Sent!');
    }else{
      die('Failed to send email');
    } 

http://groups.google.com/group/cake-php/browse_thread/thread/93a9c9467733fe38?pli=1

答案 1 :(得分:2)

将以下脚本放在 AppController.php

 function sendMailWithAttachment($template = null, $to_email = null, $from_email = null, $subject = null, $contents = array()) {
        $from_email = 'noreply@xyz.com';
        $email = new CakeEmail();
        $result = $email->template($template, 'default')
                ->emailFormat('html')
                ->to($to_email)
                ->from($from_email)
                ->subject($subject)
            ->attachments('your-server-path/filename.extenstion')
                ->viewVars($contents);
        if ($email->send('default')) {
            return true;
        }
        return false;
    }

之后,只需在任何控制器中调用方法sendMailWithAttachment(),如

$this->sendMailWithAttachment('tmpl', 'test@abc.com','noreply@xyz.com', 'Subject', $tmpl_DATA_IN_ARRAY);