php - 如何在附加并发送文件后取消链接? (使用 Mail::queue 时)

标签 php laravel-4 swiftmailer

我从立即发送邮件切换到将它们添加到队列中,这是我的代码,$attachments 是一个临时路径数组,我已经注释掉了我尝试过的内容,这会引发有关文件不存在的错误。

Mail::queue($view, $data, function(\Illuminate\Mail\Message $message) use($mail,$attachments){
    foreach($mail->getRecipients() as $recipient){
        $message->to($recipient);
    }
    $message->subject($mail->getSubject());
    foreach($attachments as $attachment){
        $message->attach($attachment);
        //this deletes the attachment before being sent
        //unlink($attachment);
    }
});
/* This code only works when using Mail::send() instead of Mail:queue()
foreach($attachments as $attachment){
    unlink($attachment);
}
*/

基本上我想在邮件发送后清理并删除我的临时附件。我猜这不适用于开箱即用的 laravel 邮件解决方案。如何触发代码 post-queue-mail-sent

最佳答案

稍微扩展一下 Deric Lima 的回答,您不一定需要为此创建一个新的 Job 类。您也可以使用 Mailable 对象来完成。只需覆盖 send 方法即可。

/**
 * @param MailerContract $mailer
 */
public function send(MailerContract $mailer)
{
    parent::send($mailer);

    //$this->clearAttachments is something you can defined in your constructor,
    //making it the responsibility of whatever is applying the attachment
    //to know whether it needs to remain in tact after the email is transmitted.
    if ($this->clearAttachments) {
        foreach ($this->attachments as $attachment) {
            if (\File::exists($attachment['file'])) {
                \File::delete($attachment['file']);
            }
        }
    }
}

就我个人而言,我会创建一个所有其他 Mailable 类都扩展的 BaseMailable 类,而不是直接使用 Illuminate\Mail\Mailable .那么从此以后您甚至不必担心它。

关于php - 如何在附加并发送文件后取消链接? (使用 Mail::queue 时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23637495/

相关文章:

php - 正则表达式用于根据某些条件验证数字

php - Dropzone 和非对象错误

laravel - Vagrant Puppet 配置 - Pear 包

php - SwiftMailer 在电子邮件正文中附加图像

php - 电子邮件帮助 - php mail() 或 SMTP

进行简单的 sql 查询时,Java vs PHP

php - 留空时多个下拉菜单查询结果

php - 如何使用 .htaccess 在 Laravel 4 中选择环境?

Symfony2、奏鸣曲、用户包 : Send email when user is enable on update

java - Facebook GRAPH API - 登录绕过。代币安全