From fe59537bc6a517f52345668c56f645813d2214d0 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Thu, 16 Apr 2020 18:03:09 +0200 Subject: [PATCH] use twig renderer --- src/Controller/FramadateController.php | 82 +++++++++++++++++++++++++- src/Controller/PollController.php | 4 +- src/Service/MailService.php | 11 +--- 3 files changed, 84 insertions(+), 13 deletions(-) diff --git a/src/Controller/FramadateController.php b/src/Controller/FramadateController.php index 7baeb59..3b067f7 100644 --- a/src/Controller/FramadateController.php +++ b/src/Controller/FramadateController.php @@ -2,11 +2,87 @@ namespace App\Controller; -use App\Service\MailService; +use App\Entity\Owner; +use App\Entity\Poll; +use JMS\Serializer\Type\Exception\Exception; +use Swift_Message; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class FramadateController extends AbstractController { - public function __construct( MailService $mail_service ) { - $this->mail_service = $mail_service; + + public function __construct( \Swift_Mailer $mailer ) { + $this->mail_service = $mailer; + } + + /** + * generic way to send email with html template + * + * @param $config + * + * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface + */ + public function sendMailWithVars( $config ) { + + if ( ! isset( $config[ 'poll' ] ) ) { + $config[ 'poll' ] = new Poll(); + } + $emailChoicesTemplates = [ + 'creation_poll' => 'creation-mail.html.twig', + 'edit_poll' => 'modification-notification-mail.html.twig', + 'creation_poll_admin' => 'author-mail.html.twig', + 'owner_list' => 'owner-list.html.twig', + 'expiration' => 'expiration-mail.html.twig', + 'creation_comment' => 'comment-notification.html.twig', + 'creation_vote' => 'vote-notification.html.twig', + ]; + $emailChoicesTitles = [ + 'creation_poll' => 'Framadate | Création de sondage - lien public - ' . $config[ 'poll' ]->getTitle(), + 'edit_poll' => 'Framadate | Modification de sondage - ' . $config[ 'poll' ]->getTitle(), + 'creation_poll_admin' => 'Framadate | Création de sondage - lien admin - ', + 'owner_list' => 'Framadate | Vos sondages créés', + 'expiration' => 'Framadate | Notice d\'expiration du sondage ' . $config[ 'poll' ]->getTitle(), + 'creation_comment' => 'Framadate | Commentaire de "' . $config[ 'owner' ]->getPseudo() . '" - sondage ' . $config[ 'poll' ]->getTitle(), + 'creation_vote' => 'Framadate | Vote de "' . $config[ 'owner' ]->getPseudo() . '" - sondage ' . $config[ 'poll' ]->getTitle(), + ]; + // Create a message + $htmlbody = $this->renderView( + $config[ 'email_template' ], + $config + ); + $message = ( new Swift_Message( 'Wonderful Subject from FAT computer' ) ) + ->setContentType( "text/html" ) + ->setCharset( 'UTF-8' ) + ->setFrom( [ 'contact@framadate-api.cipherbliss.com' ] ) + ->setTo( [ 'contact@cipherbliss.com' ] ) + ->setBody( $htmlbody, 'text/html' ); + + +// Send the message + $numSent = $this->mail_service->send( $message ); + $this->numSent = $numSent; + + + } + + + /** + * send created polls to an owner + * + * @param Owner $owner + * + * @return int|void + * @throws Exception + * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface + */ + public function sendOwnerPollsAction( Owner $owner ) { + + $config = [ + 'owner' => $owner, + 'title' => 'Framadate | Mes sondages', + 'email_template' => 'emails/owner-list.html.twig', + ]; + $this->sendMailWithVars( $config ); + + return 1; } } diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 94d936f..3098095 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -266,9 +266,9 @@ class PollController extends FramadateController { $poll = $foundOwner->getPolls()[ 0 ]; $comment = $foundOwner->getComments()[ 0 ]; - $sent = $this->mail_service->sendCreationMailAction( $foundOwner, $poll ); + $sent = $this->sendOwnerPollsAction( $foundOwner, $poll ); if ( $sent ) { - return $this->json( [ "message" => $this->mail_service->numSent. "test email sent to ".$foundOwner->getEmail()."!" ], 200 ); + return $this->json( [ "message" => "test email sent to ".$foundOwner->getEmail()."!" ], 200 ); } } diff --git a/src/Service/MailService.php b/src/Service/MailService.php index adb4a9a..5f6c72c 100644 --- a/src/Service/MailService.php +++ b/src/Service/MailService.php @@ -12,7 +12,6 @@ use Exception; use Swift_Mailer; use Swift_Message; use Swift_SmtpTransport; -use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; class MailService { @@ -29,17 +28,13 @@ class MailService { private $numSent; // public function __construct( EntityManagerInterface $entityManager , Mailer $mailer) { - /** - * @var EngineInterface - */ + private $templating; public function __construct( - EntityManagerInterface $entityManager, - EngineInterface $templating + EntityManagerInterface $entityManager ) { - $this->em = $entityManager; - $this->templating = $templating; + $this->em = $entityManager; // Create the Transport $transport = new Swift_SmtpTransport( 'localhost', 25 );