开发者

Dynamic email attachments in cakephp

Is it possible to send an email with an dynamically generated attachment?

I tried it this way:

$this->Email->attachments = array(
    'reservation.ics' => array(
        'controller' => 'reservations', 
        'action' => 'ical',
        'ext' =&开发者_C百科gt; 'ics',
        $this->data['Reservation']['id']
    )
);

But it didn't work.


attachments only takes paths to local files on the server, not URLs. You need to render your attachment to a temporary file, then attach it.

In your controller, this could roughly look like this:

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

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

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


There are another method to send attachment. firstly store this file on the server then use the server path to send. In the below example I skip the code to store attachment file. There is code for attachment only.

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');
 }


 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜