【问题标题】:CakePHP : send email with attachment via consoleCakePHP:通过控制台发送带有附件的电子邮件
【发布时间】:2012-06-11 22:07:54
【问题描述】:

我正在尝试通过控制台/cron 使用 CakePHP 1.3 电子邮件组件发送电子邮件。 电子邮件已发送和接收,但没有附件。

通过表单完成后,电子邮件将与附件一起成功发送。 我已尝试添加
$this->Email->filePaths,
(来自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']);
}

所以,基本上我的问题是,当我通过控制台/cron 运行 shell 脚本时,如何获取带有附件的电子邮件。

提前谢谢你。

【问题讨论】:

  • 如果您正在通过控制台排队和发送电子邮件,您可能会对这个插件感兴趣。它可能会减少你的一些工作。 github.com/jeremyharris/queue_email
  • 回到 cron 作业后,我会看看你的插件。完成后应在此处更新结果。感谢您的宝贵时间。

标签: cakephp console email-attachments


【解决方案1】:

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

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

【讨论】:

  • 您是否尝试过只使用完整的附件路径而不是使用Email->filePaths var?喜欢$this->Email->attachments = array(WWW_ROOT . 'files' . DS . 'email_attachments' . DS . $newEmail['attachment_tmp'])。此外,使用file_exists() 验证文件是否存在于该位置。
  • 是的,我有并且该文件确实存在。但电子邮件中仍然没有附件。期待试用您的插件并希望能够解决问题(并希望阐明我的插件无法正常工作的真正原因)。
  • 您好,该项目已被取消,我已继续前进。但是,如果有人对此有答案,将不胜感激。谢谢
猜你喜欢
  • 1970-01-01
  • 2020-01-10
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 2014-07-23
  • 2015-09-09
相关资源
最近更新 更多