CakePHP:通过控制台发送带附件的电子邮件

时间:2012-06-08 02:51:29

标签: cakephp console email-attachments

我正在尝试通过console / cron发送带有CakePHP 1.3电子邮件组件的电子邮件。 电子邮件发送出去&收到了,但没有附件。

通过表单完成后,电子邮件将与附件一起成功发送。 我试过添加
    的 $这 - >的电子邮件 - >文件路径下,
(来自How do I send an email with an attachment in CakePHP 2.0?
但附件仍未发送。

我的代码如下:

$email =& new EmailComponent();
$email->reset();
$email->initialize($controller);        
$email->delivery = $emailConfigurations['delivery'];        
$email->from = $emailConfigurations['from'];
$email->replyTo = $emailConfigurations['replyTo'];
$email->return = $emailConfigurations['return'];
$email->template = 'default';
$email->sendAs = $emailConfigurations['sendAs'];

if (strcasecmp($email->delivery, 'smtp') == 0) {
    $email->smtpOptions = array(
        'timeout' => $emailConfigurations['smtpTimeout'],
        'port' => $emailConfigurations['smtpPort'],
        'host' => $emailConfigurations['smtpHost'],
        'username' => $emailConfigurations['smtpUsername'],
        'password' => $emailConfigurations['smtpPassword']
    );
}

$email->to = $newEmail['mail_to'];
$email->subject = $newEmail['message_title'];

if ($newEmail['attachment_name'] && $newEmail['attachment_tmp']) {
    $attachedFilePath = WWW_ROOT . 'files' . DS .  'email_attachments' . DS ;
    $attachedFile = $newEmail['attachment_tmp'];

    $this->Email->filePaths  = array($attachedFilePath); 
    $this->Email->attachments = array($attachedFile);
}

if($email->send($newEmail['message'])){
    $this->out(date('Y-m-d H:i:s')." Email sent : ".$newEmail['id']);
} else {
    $this->out(date('Y-m-d H:i:s')." Email not sent : ".$newEmail['id']);
}

所以,基本上我的问题是当我通过console / cron运行shell脚本时,如何通过附件发送电子邮件。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

从路径列表中删除尾随DS,因为Cake不修剪它:

$attachedFilePath = WWW_ROOT . 'files' . DS .  'email_attachments';