Email cakephp中的动态电子邮件附件
是否可以发送带有动态生成附件的电子邮件 我这样试过:Email cakephp中的动态电子邮件附件,email,cakephp,dynamic,cakephp-1.3,attachment,Email,Cakephp,Dynamic,Cakephp 1.3,Attachment,是否可以发送带有动态生成附件的电子邮件 我这样试过: $this->Email->attachments = array( 'reservation.ics' => array( 'controller' => 'reservations', 'action' => 'ical', 'ext' => 'ics', $this->data['Reservation']['id']
$this->Email->attachments = array(
'reservation.ics' => array(
'controller' => 'reservations',
'action' => 'ical',
'ext' => 'ics',
$this->data['Reservation']['id']
)
);
但它不起作用。附件只获取服务器上本地文件的路径,而不获取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'
);
附件仅获取服务器上本地文件的路径,而不是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'
);
还有另一种发送附件的方法。首先将此文件存储在服务器上,然后使用服务器路径发送。在下面的示例中,我跳过存储附件文件的代码。只有附件才有代码
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');
}
}
还有另一种发送附件的方法。首先将此文件存储在服务器上,然后使用服务器路径发送。在下面的示例中,我跳过存储附件文件的代码。只有附件才有代码
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');
}
}
Thx,它有效!我只需要设置一些变量,因为render不接受参数,但我也在我的电子邮件中使用这些变量,所以这很好。如果你不想在磁盘上创建硬文件,你也可以查看extends CakeEmail和$this->email->AddBlobatachment方法。Thx,它可以工作!我只需要设置一些变量,因为render不接受参数,但我也在电子邮件中使用这些变量,所以这很好。如果你不想在磁盘上创建硬文件,你也可以查看extends CakeEmail和$this->email->AddBlobatachment方法。