我从立即发送邮件切换到将它们添加到队列中,这是我的代码,$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/