Swiftmailer发送后删除附件

Eri*_*och 3 php swiftmailer symfony

我发送电子邮件与Symfony 2.1Swiftmailer后,我试图删除附件文件,但如果我在返回响应对象(重定向)之前删除该文件,电子邮件不会发送.

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

例如:

<?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'));
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

jam*_*s_t 10

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

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

您可以在send()通话后和通话前添加此内容,unlink()以模仿发送电子邮件的行为

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

        $spool = $transport->getSpool();

        $spool->flushQueue($this->container->get('swiftmailer.transport.real'));
Run Code Online (Sandbox Code Playgroud)