2012-04-06 130 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); 
相關問題