cakephp中的动态电子邮件附件

时间:2011-03-27 12:25:47

标签: email cakephp dynamic cakephp-1.3 attachment

是否可以发送包含动态生成附件的电子邮件?

我试过这种方式:

$this->Email->attachments = array(
    'reservation.ics' => array(
        'controller' => 'reservations', 
        'action' => 'ical',
        'ext' => 'ics',
        $this->data['Reservation']['id']
    )
);

但它不起作用。

2 个答案:

答案 0 :(得分:3)

attachments仅获取服务器上本地文件的路径,而不是URL。您需要将附件呈现给临时文件,然后附加它。

在你的控制器中,这可能大致如下:

$this->autoRender = false;
$content = $this->render();

file_put_contents(
    TMP . 'reservation' . $id . '.ics',
    $content
);

$this->Email->attachments = array(
    'reservation.ics' => TMP . 'reservation' . $id . '.ics'
);

答案 1 :(得分:1)

还有另一种发送附件的方法。首先将此文件存储在服务器上,然后使用服务器路径发送。在下面的示例中,我跳过代码来存储附件文件。只有附件代码。

Class EmailController extends AppController { 


var $name="Email"; 
 var $components = array ('Email');
 var $uses = NULL;
 function beforeFilter() {
        parent::beforeFilter(); 
 $this->Auth->allow(array(*));
 } 
 function EmailSend(){
 $Path = WWW_ROOT."img";
 $fileName = 'test.jpg';
 $this->Email->from    = 'Amit Jha<amit@mail.com>';
       $this->Email->to      = 'Test<test@test.com>';
       $this->Email->subject = 'Test Email Send With Attacment';
       $this->Email->attachments = array($Path.$fileName);
      $this->Email->template = 'simple_message';
       $this->Email->sendAs = 'html';
       if($this->Email->send()){
 $this->session->setFlash("Email Send Successfully");
 $this->redirect('somecontroller/someaction');
 }


 }