Swiftmailer remove attachment after send

送分小仙女□ 提交于 2019-12-01 07:06:17

问题


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.

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

For example:

<?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'));

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

Thanks!


回答1:


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.

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'));



回答2:


I am not sure, but the message spool might cause this problem. In SF2 memory spool is used by default, which means the messages are being sent on the kernel terminate event.

So you'd have to flush the spool before deleting the file.

If this is the cause of your problem, look here for a well explained solution: http://sgoettschkes.blogspot.de/2012/09/symfony-21-commands-and-swiftmailer.html




回答3:


In order to complete the very good answer of james_t, if you use multiple mailers some changes are needed.

Replace

//  Default mailer
$mailer = $this->container->get('mailer');

$subject  = '...';
$from     = '...';
$to       = '...';
$body     = '...';

$message = \Swift_Message::newInstance()
    ->setSubject($subject)
    ->setFrom($from)
    ->setTo($to)
    ->setBody($body, 'text/html')
;

//  Put e-mail in spool
$result = $mailer->send($message);

//  Flush spool queue
$transport      = $mailer->getTransport();  
$spool          = $transport->getSpool();
$realTransport  = $this->container->get('swiftmailer.transport.real')
$spool->flushQueue($realTransport);

By

//  Custom mailer
$mailerServiceName  = 'myCustomMailer';
$customMailer       = $this->container->get("swiftmailer.mailer.".$mailerServiceName);

$subject  = '...';
$from     = '...';
$to       = '...';
$body     = '...';

$message = \Swift_Message::newInstance()
    ->setSubject($subject)
    ->setFrom($from)
    ->setTo($to)
    ->setBody($body, 'text/html')
;

//  Put e-mail in spool
$result = $customMailer->send($message);

//  Flush spool queue
$transport      = $customMailer->getTransport();  
$spool          = $transport->getSpool();
$realTransport  = $this->container->get('swiftmailer.mailer.'.$mailerServiceName.'.transport.real');
$spool->flushQueue($realTransport);


来源:https://stackoverflow.com/questions/15643027/swiftmailer-remove-attachment-after-send

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!