Я хочу отправить электронное письмо с прикрепленным к нему файлом с помощью 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);
}
У кого-нибудь есть идеи по ее решению?