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 %} +
+ + + {% autoescape %} + {{ comment.text }} + {% endautoescape %} + ++