From 9b7f2ebd32f77c128b515b4a67ba43069ea27118 Mon Sep 17 00:00:00 2001 From: tykayn Date: Tue, 14 Apr 2020 18:09:03 +0200 Subject: [PATCH] send html template --- .env | 2 +- src/Controller/DefaultController.php | 30 ++++++------- src/Service/MailService.php | 65 ++++++++++++++++++++++++---- 3 files changed, 72 insertions(+), 25 deletions(-) diff --git a/.env b/.env index 00049f9..5ca8199 100755 --- a/.env +++ b/.env @@ -38,7 +38,7 @@ CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$ # For Gmail as a transport, use: "gmail://username:password@localhost" # For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode=" # Delivery is disabled by default via "null://localhost" -MAILER_URL=MAILER_URL=sendmail://framadate-api.cipherbliss.com +MAILER_URL=sendmail://framadate-api.cipherbliss.com # set the support email who will answer users in case of emergency SUPPORT_EMAIL=admin_framadate@yopmail.com ###< symfony/swiftmailer-bundle ### diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index fdc9fbc..e3959b8 100755 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -3,9 +3,9 @@ namespace App\Controller; use App\Entity\Owner; +use App\Service\MailService; use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Route; -use Swift_Message; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; @@ -15,7 +15,14 @@ use Symfony\Component\HttpFoundation\JsonResponse; * @Route("/api/v1",name="api_") */ class DefaultController extends AbstractController { + /** + * @var MailService + */ + private $mail_service; + public function __construct(MailService $mail_service) { + $this->mail_service = $mail_service; + } /** * Send a mail with all the data to one user @@ -33,28 +40,20 @@ class DefaultController extends AbstractController { $repository = $this->getDoctrine()->getRepository( Owner::class ); // find user by email - $founduser = $repository->findOneBy( [ 'email' => $email ] ); + $founduser = $repository->findOneBy( [ 'email' => $email ] ); if ( $founduser ) { $polls = $founduser->getPolls(); $templateVars = [ 'owner' => $founduser, 'polls' => $polls, - 'title' => 'Mes sondages - '.$email, + 'title' => 'Mes sondages - ' . $email, ]; - $message = ( new Swift_Message( 'Framadate - mes sondages' ) ) - ->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' ) - ->setTo( $founduser->getEmail() ) - ->setBody( - $this->renderView( - 'emails/owner-list.html.twig', - $templateVars, - 'text/html' - ) - ); // send email - $mailer->send( $message ); + $this->mail_service->sendOwnerPollsAction(); + + return $this->json( [ 'message' => 'mail succefully sent to user ' . $email, @@ -63,8 +62,7 @@ class DefaultController extends AbstractController { 200 ); - - } else { // user not found case + } else { // user not found case return $this->json( [ 'message' => 'no user found for email ' . $email, 'data' => '', diff --git a/src/Service/MailService.php b/src/Service/MailService.php index 99a2797..1856633 100644 --- a/src/Service/MailService.php +++ b/src/Service/MailService.php @@ -9,12 +9,24 @@ use App\Entity\Poll; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Swift_Message; +use Symfony\Bridge\Twig\Mime\TemplatedEmail; +use Symfony\Component\Mime\Address; class MailService { - public function __construct(EntityManagerInterface $entityManager) { + /** + * @var EntityManagerInterface + */ + private $em; + /** + * @var \Swift_Mailer + */ + private $mailer; + + public function __construct(EntityManagerInterface $entityManager, \Swift_Mailer $mailer) { $this->em = $entityManager; + $this->mailer = $mailer; } public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, \Swift_Mailer $mailer ) { @@ -47,16 +59,53 @@ class MailService { ->setContentType('text/html') ->setCharset('UTF-8') ->setTo( $admin_user->getEmail() ) - ->setBody( - $this->renderView( - $templateVars[ 'email_template' ], - $templateVars - ), - 'text/html' - ); + ->htmlTemplate($templateVars[ 'email_template' ]) + ->context( $templateVars); // send email return $mailer->send( $message ); } + + /** + * send created polls to an owner + * @param Owner $foundOwner + * + * @return int + * @throws \Exception + */ + public function sendOwnerPollsAction( Owner $foundOwner ) { + $em = $this->em->getRepository( Owner::class ); + $admin_user = $foundOwner; + + + // anti spam , limit to every minute + $lastSend = $admin_user->getRequestedPollsDate(); + $now = new \DateTime(); + + if ( date_diff( $lastSend, $now ) < 60 ) { + // too soon! + die( 'too soon!' ); + } + $admin_user->setRequestedPollsDate( $now ); + $em->persist( $admin_user ); + $em->flush(); + $titleEmail = 'Framadate | Mes sondages'; + + $templateVars = [ + 'owner' => $admin_user, + 'title' => $titleEmail, + 'email_template' => 'emails/owner-polls.html.twig', + ]; + + $email = ( new TemplatedEmail( $titleEmail ) ) + ->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' ) + ->to( new Address($admin_user->getEmail() ) ) + ->htmlTemplate($templateVars[ 'email_template' ]) + ->context( $templateVars); + + // send email + return $this->mailer->send( $email ); + + } }