【问题标题】:Dynamic email attachments in cakephpcakephp 中的动态电子邮件附件
【发布时间】:2011-07-23 21:08:27
【问题描述】:

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

我是这样尝试的:

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

但它没有用。

【问题讨论】:

    标签: email cakephp dynamic cakephp-1.3 attachment


    【解决方案1】:

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

    【讨论】:

    • 谢谢,它有效!我只需要 set() 一些变量,因为 render() 不接受参数,但我也在电子邮件中使用了这些变量,所以没关系。
    • 如果您不想在磁盘上创建“硬文件”,您还可以查看EmailLib(扩展CakeEmail)和$this->Email->addBlobAttachment() 方法。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多