From da39373a983dd10477838523b7c7a11cad0f3502 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Sun, 12 Apr 2020 17:28:30 +0200 Subject: [PATCH] :zap: mail having containers --- src/Controller/PollController.php | 41 +++++++-------- src/Service/MailService.php | 52 +++++++++++++++++++ templates/emails/author-mail.html.twig | 17 ++++-- .../comment-notification-mail.html.twig | 4 -- .../emails/comment-notification.html.twig | 21 ++++++++ templates/emails/expiration-mail.html.twig | 2 +- .../modification-notification-mail.html.twig | 8 ++- templates/emails/owner-list.html.twig | 3 -- templates/emails/partial/poll.html.twig | 4 +- 9 files changed, 113 insertions(+), 39 deletions(-) create mode 100644 src/Service/MailService.php delete mode 100644 templates/emails/comment-notification-mail.html.twig create mode 100644 templates/emails/comment-notification.html.twig diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 3114b33..7237064 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -5,13 +5,13 @@ namespace App\Controller; use App\Entity\Choice; use App\Entity\Owner; use App\Entity\Poll; +use App\Service\MailService; use FOS\RestBundle\Controller\Annotations\Delete; use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Put; use FOS\RestBundle\Controller\Annotations\Route; use JMS\Serializer\SerializerBuilder; -use Swift_Message; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -138,7 +138,7 @@ class PollController extends AbstractController { * * @return JsonResponse */ - public function newPollAction( Request $request, \Swift_Mailer $mailer ) { + public function newPollAction( Request $request, \Swift_Mailer $mailer, MailService $mail_service ) { $data = $request->getContent(); @@ -226,7 +226,7 @@ class PollController extends AbstractController { $precision = 'from an existing user : ' . $foundOwner->getEmail(); } - $this->sendCreationMailAction( $foundOwner, $newpoll, $mailer ); + $mail_service->sendCreationMailAction( $foundOwner, $newpoll ); return $this->json( [ 'message' => 'you created a poll ' . $precision, @@ -242,7 +242,7 @@ class PollController extends AbstractController { /** * @Get( - * path = "/mail/test-mail-poll", + * path = "/mail/test-mail-poll/{emailChoice}", * name = "test-mail-poll", * ) * @@ -255,34 +255,29 @@ class PollController extends AbstractController { * @return int */ // public function sendCreationMailAction( Owner $admin_user, Poll $poll, \Swift_Mailer $mailer) { - public function sendCreationMailAction( \Swift_Mailer $mailer ) { - + public function testSendCreationMailAction( MailService $mail_service, $emailChoice = 'creation_comment' ) { $em = $this->getDoctrine()->getRepository( Owner::class ); - $admin_user = $em->find( 1 ); - $poll = $admin_user->getPolls()[ 0 ]; + $foundOwner = $em->find( 1 ); + $poll = $foundOwner->getPolls()[ 0 ]; + $comment = $foundOwner->getComments()[ 0 ]; + + $emailChoicesTemplates = [ + 'creation_poll' => 'creation-mail.html.twig', + 'creation_comment' => 'comment-notification.html.twig', + ]; + +// $mail_service->sendCreationMailAction( $foundOwner, $poll ); $templateVars = [ - 'owner' => $admin_user, + 'owner' => $foundOwner, + 'comment' => $comment, 'poll' => $poll, 'url' => $poll->getCustomUrl(), 'title' => 'Création de sondage - ' . $poll->getTitle(), - 'email_template' => 'emails/creation-mail.html.twig', + 'email_template' => 'emails/' . $emailChoicesTemplates[ $emailChoice ], ]; - $message = ( new Swift_Message( 'Framadate - mes sondages' ) ) - ->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' ) - ->setTo( $admin_user->getEmail() ) - ->setBody( - $this->renderView( - $templateVars[ 'email_template' ], - $templateVars - ) - ); - - // send email -// return $mailer->send( $message ); return $this->render( $templateVars[ 'email_template' ], $templateVars ); - } diff --git a/src/Service/MailService.php b/src/Service/MailService.php new file mode 100644 index 0000000..eb229aa --- /dev/null +++ b/src/Service/MailService.php @@ -0,0 +1,52 @@ +getDoctrine()->getRepository( Owner::class ); + $admin_user = $em->find( 1 ); + $poll = $admin_user->getPolls()[ 0 ]; + + + // 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(); + + $templateVars = [ + 'owner' => $admin_user, + 'poll' => $poll, + 'url' => $poll->getCustomUrl(), + 'title' => 'Création de sondage - ' . $poll->getTitle(), + 'email_template' => 'emails/creation-mail.html.twig', + ]; + + $message = ( new Swift_Message( 'Framadate - mes sondages' ) ) + ->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' ) + ->setTo( $admin_user->getEmail() ) + ->setBody( + $this->renderView( + $templateVars[ 'email_template' ], + $templateVars + ) + ); + + // send email + return $mailer->send( $message ); + + } +} diff --git a/templates/emails/author-mail.html.twig b/templates/emails/author-mail.html.twig index 3611148..d97b292 100644 --- a/templates/emails/author-mail.html.twig +++ b/templates/emails/author-mail.html.twig @@ -1,11 +1,18 @@ {#[Framadate][Réservé à l'auteur] Sondage: TESSSSSSSSSST#} -Ce message ne doit PAS être diffusé aux sondés. Il est réservé à l'auteur du sondage. +{% extends 'email-base.html.twig' %} +{% block content %} +

+ + Ce message ne doit PAS être diffusé aux sondés. Il est réservé à l'auteur du sondage. +


-Vous pouvez modifier ce sondage à l'adresse suivante : +

+ Vous pouvez modifier ce sondage à l'adresse suivante : +


-{% include 'partial/admin_link.html.twig' %} +{% include 'emails/partial/admin_link.html.twig' %}
-Pour partager votre sondage aux participants, utilisez son lien d'accès public. +Pour partager votre sondage aux participants, utilisez son lien d'accès public que vous avez reçu dans un autre email.
{% if poll.password %} @@ -14,3 +21,5 @@ Pour partager votre sondage aux participants, utilisez son lien d'accès public. {% else %} {% endif %} + +{% endblock %} diff --git a/templates/emails/comment-notification-mail.html.twig b/templates/emails/comment-notification-mail.html.twig deleted file mode 100644 index e974021..0000000 --- a/templates/emails/comment-notification-mail.html.twig +++ /dev/null @@ -1,4 +0,0 @@ -{#[Framadate] Notification d'un sondage : TESSSSSSSSSST#} -{{ pseudo }} vient de rédiger un commentaire. -
-Vous pouvez retrouver votre sondage avec le lien suivant : {{ url }} diff --git a/templates/emails/comment-notification.html.twig b/templates/emails/comment-notification.html.twig new file mode 100644 index 0000000..51aa816 --- /dev/null +++ b/templates/emails/comment-notification.html.twig @@ -0,0 +1,21 @@ +{#[Framadate] Notification d'un sondage : TESSSSSSSSSST#} +{% extends 'email-base.html.twig' %} +{% block content %} + + {{ owner.pseudo }} + + vient de rédiger un commentaire. +
+ + + {% autoescape %} + {{ comment.text }} + {% endautoescape %} + +
+
+ Vous pouvez retrouver votre sondage avec le lien suivant : + {% include 'emails/partial/admin_link.html.twig' %} + {% include 'emails/partial/public_link.html.twig' %} + +{% endblock %} diff --git a/templates/emails/expiration-mail.html.twig b/templates/emails/expiration-mail.html.twig index b4bad72..70634c6 100644 --- a/templates/emails/expiration-mail.html.twig +++ b/templates/emails/expiration-mail.html.twig @@ -6,5 +6,5 @@ Ce sondage va bientôt expirer dans 1 jour, il ne sera plus possible d'y voter. Dans 31 jours il sera supprimé. Vous pouvez exporter ses données à tout moment en vous rendant à ce lien pour l'administrer: -{{ url }} + {% include 'emails/partial/admin_link.html.twig' %} {% endblock %} diff --git a/templates/emails/modification-notification-mail.html.twig b/templates/emails/modification-notification-mail.html.twig index 2e98e6e..797eb46 100644 --- a/templates/emails/modification-notification-mail.html.twig +++ b/templates/emails/modification-notification-mail.html.twig @@ -1,4 +1,8 @@ {#[Framadate] Participation au sondage : TESSSSSSSSSST#} -Quelqu'un vient de modifier votre sondage accessible au lien suivant: +{% extends 'email-base.html.twig' %} +{% block content %} + Quelqu'un vient de modifier votre sondage accessible au lien suivant:
-{{ url }} + {% include 'emails/partial/admin_link.html.twig' %} + +{% endblock %} diff --git a/templates/emails/owner-list.html.twig b/templates/emails/owner-list.html.twig index 77b1900..1fec181 100644 --- a/templates/emails/owner-list.html.twig +++ b/templates/emails/owner-list.html.twig @@ -17,10 +17,7 @@ diff --git a/templates/emails/partial/poll.html.twig b/templates/emails/partial/poll.html.twig index a0243ac..347ca45 100644 --- a/templates/emails/partial/poll.html.twig +++ b/templates/emails/partial/poll.html.twig @@ -28,12 +28,12 @@ lien à donner aux votants: - {% include 'public_link.html.twig' %} + {% include 'emails/partial/public_link.html.twig' %}
administration: - {% include 'admin_link.html.twig' %} + {% include 'emails/partial/admin_link.html.twig' %}