I want to send an email with a file attached to it, with swiftMailer, and after that, I want to delete the same file. But when I do it, it doesnt work. My file is not deleted, the mail is not sent, and there is a weird thing. Two emails are created. Here is the controller:
public function rentIsPaid(
RentRelease $rentRelease,
PdfGenerator $pdfGenerator,
MonthlyMailer $monthlyMailer
): Response {
if (!$this->isGranted('EDIT_RENT_RELEASE', $rentRelease)) {
$this->addFlash('danger', 'Vous n\'etes pas autorisé à effectuer cette action.');
return $this->redirectToRoute('rent_release_index');
}
$rentRelease->setStatus('Payé');
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($rentRelease);
$entityManager->flush();
$pdfGenerator->generateRentReleasePdf($rentRelease);
$monthlyMailer->sendRentReleaseToLessees($rentRelease);
$filesystem = new Filesystem();
$pdfFile = $rentRelease->getPdf();
$filesystem->remove("generated/pdf/$pdfFile"); //if I don't remove the file but I keep the attachment, it works
$entityManager = $this->getDoctrine()->getManager();
$rentRelease->setPdf(null);
$entityManager->persist($rentRelease);
$entityManager->flush();
return $this->redirectToRoute('rent_release_index');
}
the services are here :
public function generateRentReleasePdf(RentRelease $rentRelease)
{
$currentDate = new DateTime();
$currentDate = $currentDate->format('m-Y');
if ($rentRelease->getStatus() === 'Payé') {
$propertyName = $rentRelease->getPropertyName();
$propertyName= str_replace(' ', '_', $propertyName);
$lesseeName = str_replace(' ', '-', $rentRelease->getLesseeName());
$fileName = $propertyName . '_' . $lesseeName . '_' . date("m-Y") . '_';
$fileName = $fileName . bin2hex(random_bytes(5)) . '.pdf';
$fileName = $this->removeAccents($fileName);
$html = $this->twig->render('rent_release/pdf.html.twig', [
'rent_release' => $rentRelease,
'current_date' => $currentDate,
]);
$this->knpSnappyPdf->generateFromHtml("$html", "generated/pdf/$fileName");
$rentRelease->setPdf($fileName);
$this->manager->persist($rentRelease);
$this->manager->flush();
}
}
public function sendRentReleaseToLessees(RentRelease $rentRelease)
{
$mail = $rentRelease->getRentRelease()->getEmail();
$name = $rentRelease->getLesseeName();
$owner = $rentRelease->getUserRentRelease()->getName();
$owner = $owner . ' ' . $rentRelease->getUserRentRelease()->getLastName();
$property = $rentRelease->getPropertyName();
$pdf = $rentRelease->getPdf();
$message = (new \Swift_Message('Votre quittance de loyer'))
->setFrom(getenv('MAILER_FROM_ADDRESS'))
->setTo("$mail")
->setBody(
$this->twig->render(
'emails/rentReleaseMail.html.twig',
[
'name' => $name,
'owner' => $owner,
'property' => $property,
]
),
'text/html'
)
->attach(Swift_Attachment::fromPath("generated/pdf/$pdf")); //if I don't set attachment but I keep the delete, it works
$this->mailer->send($message);
}
Does anyone has any idea to solve it ?
I think your problem is caused by symfony swiftmailer spooling
When you use spooling to store the emails to memory, they will get sent right before the kernel terminates (https://symfony.com/doc/current/email/spool.html#spool-using-memory)
It means that emails are not sent directly, however since you delete the file directly after calling $monthlyMailer->sendRentReleaseToLessees($rentRelease); There is a problem with the attachment.
Try to change your swiftmailer configuration to disable spooling, or to create an event listener to symfony kernel.terminate event and to delete the file from your event listener.
To disable email spooling :
Apparently the only way to disable email spooling is to completely leave out the spool entry from your configuration file