附加并发送文件后如何取消链接? (使用Mail :: queue时) [英] How to unlink files after they have been attached and sent? (When using Mail::queue)

查看:81
本文介绍了附加并发送文件后如何取消链接? (使用Mail :: queue时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从立即发送邮件切换到将其添加到队列中,这是我的代码,$attachments是一组临时路径,我已注释掉我尝试过的内容,这会导致文件不正确的错误现存的.

I switched from sending my mails immediately to adding them to the queue, here is my code, the $attachments is an array of temporary paths, I've commented out what I've tried, which throws errors about files not existing.

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?

Basically I want to clean up and remove my temporary attachments after the mail was sent. I am guessing this would not work with the out of the box laravel mail solutions. How can I trigger code post-queue-mail-sent?

推荐答案

您必须等到队列处理完毕后才能删除文件.

You have to wait until the queue is processed before removing the file.

不知道队列的实现细节,很难回答您的问题,但是如果在脚本结束之前处理了队列,则可以使用register_shutdown_function http://www.php.net/manual/zh/function .register-shutdown-function.php 以运行清除功能以删除文件

Without knowing the implementation details of the queue it is hard to answer your question, but if your queue is processed before the script ends, you can use register_shutdown_function http://www.php.net/manual/en/function.register-shutdown-function.php to run a cleanup function that removes the file

register_shutdown_function(function() use (filename){
    if (file_exists($filename)) {
        unlink($filename);
    }
})

这篇关于附加并发送文件后如何取消链接? (使用Mail :: queue时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆