Swiftmailer 发送后删除附件 [英] Swiftmailer remove attachment after send

查看:32
本文介绍了Swiftmailer 发送后删除附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在使用 Symfony 2.1Swiftmailer 发送电子邮件后删除附件文件,但如果我在返回响应对象(重定向)之前删除文件,电子邮件没有发送.

I am trying to remove the attachment files after sending an email with Symfony 2.1 and Swiftmailer but if I delete the file before returning a response object (a redirect), the email does not send.

我想这是因为 symfony 在回复中发送了电子邮件,所以当电子邮件发送时,附件已经被删除了.

I suppose this is because symfony sends the email in the response, so when the email has send the attachment has been removed already.

例如:

<?php

// DefaultCotroller.php

$message = \Swift_Message::newInstance($subject)
    ->setFrom('no-reply@dasi.es')
    ->setTo($emails_to)
    ->setBody($body, 'text/html')
    ->attach(\Swift_Attachment::fromPath('backup.rar'));

$this->get('mailer')->send();

unlink('backup.rar');  // This remove the file but doesn't send the email!

return $this->redirect($this->generateUrl('homepage'));

一个选项是创建一个 crontab 来清理文件,但我不喜欢使用它.

An option is to create a crontab to clean the files, but I prefer not using it.

谢谢!

推荐答案

您可以在此处查看处理内存假脱机的代码:https://github.com/symfony/SwiftmailerBundle/blob/master/EventListener/EmailSenderListener.php

You can look at the code that processes memory spools here: https://github.com/symfony/SwiftmailerBundle/blob/master/EventListener/EmailSenderListener.php

这用于批量发送要发送的电子邮件.

This is used to batch the emails to be sent.

您可以在 send() 调用之后和 unlink() 调用之前添加它以模拟发送电子邮件的行为

You can add this after your send() call and before your unlink() call to mimic the behavior of sending an email

        $transport = $this->container->get('mailer')->getTransport();  

        $spool = $transport->getSpool();

        $spool->flushQueue($this->container->get('swiftmailer.transport.real'));

这篇关于Swiftmailer 发送后删除附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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