我想使用swiftMailer发送一个带有附件的电子邮件,此后,我想删除相同的文件。但是,当我这样做时,它不起作用。 我的文件没有被删除,邮件也没有发送,这很奇怪。 将创建两个电子邮件。 这是控制器:
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');
}
服务在这里:
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);
}
有人有解决的办法吗?
答案 0 :(得分:3)
我认为您的问题是由symfony swiftmailer假脱机引起的
当您使用假脱机将电子邮件存储到内存时,它们将在内核终止之前立即发送(https://symfony.com/doc/current/email/spool.html#spool-using-memory)
这意味着不会直接发送电子邮件,但是由于您在调用$ monthlyMailer-> sendRentReleaseToLessees($ rentRelease)之后直接删除了文件,附件有问题。
尝试更改您的swiftmailer配置以禁用假脱机,或为symfony kernel.terminate事件创建事件监听器,并从事件监听器中删除文件。
要禁用电子邮件假脱机:
显然,禁用电子邮件假脱机的唯一方法是从配置文件中完全忽略假脱机条目