2012-04-06 150 views
3

我正在尝试使用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(); 

电子邮件发送和看起来不错,但没有附件。

我在做什么错?

感谢您的任何帮助。

+0

我总是尽力接受答案,但正如马克所说,我确实有很多问题没有得到回答。也许我需要停止提出尴尬的问题! – Sharon 2012-04-06 20:28:30

回答

3

由于某些原因,除非您首先声明 filePaths,否则CakePHP将不会执行附件。

我有同样的问题,我没有设法找到这个问题很多答案 。我花了一段时间来解决问题,澄清, 我得到了它的

$this->Email->filePaths = array('/home/username/'); 
     $this->Email->attachments =array('article_button.png'); 
$this->Email->to  = '[email protected]'; 
    $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

+0

我从未遇到附件路径问题。奇怪的 – mark 2012-04-06 17:52:00

+0

谢谢你,但我还没有能够使它的工作。当我打印出临时文件的路径时,我得到/ home/content/57/4087557/tmp/phpblahblah - 所以我应该有$ email-> filePaths = array('/ home/content/57/4087557/tmp/'), 对? – Sharon 2012-04-06 20:48:51

2

把下面的脚本在工作AppController.php

function sendMailWithAttachment($template = null, $to_email = null, $from_email = null, $subject = null, $contents = array()) { 
     $from_email = '[email protected]'; 
     $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', '[email protected]','[email protected]', 'Subject', $tmpl_DATA_IN_ARRAY); 
相关问题