0

Hello I am using SwiftMailer on Symfony2, I can send emails successfuly, but now i am first making a pdf, then attaching at the mail, sending and finally deleting the pdf file.

here is my code.

$pdf->Output('cuestionario/attach/' . $nombre . '.pdf', 'F');
                        $correosFinal = array();
                        foreach ($correos as $correo) {
                            $correosFinal[$correo->getId()] = $correo->getCorreo();
                        }
                        $message = Swift_Message::newInstance()
                                ->setSubject($respuesta['nombre'])
                                ->setFrom($this->container->getParameter('mailer_user'))
                                ->setTo($correosFinal)
                                ->setBody($html, 'text/html')
                                ->attach(Swift_Attachment::fromPath(
                                        'cuestionario/attach/' . $nombre
                                        . '.pdf'));
                        $enviado = $this->get('mailer')->send($message);
                        if ($enviado) {
                            unlink('cuestionario/attach/' . $nombre . '.pdf');
                        }

The trouble is that I am deleting the file so fast that the mail say sent but hit dont really is sent....

fjbatresv
  • 137
  • 2
  • 12

1 Answers1

2

Mails are spooled, so there are not sent when you call the 'send' method. In order to force to flush the spool, you can do :

$spool = $this->container->get('mailer')->getTransport()->getSpool();
$transport = $this->container->get('swiftmailer.transport.real');
if ($spool and $transport) $spool->flushQueue($transport);               
griotteau
  • 1,772
  • 12
  • 15