From bfd938d190ffc1c40abcb8cd9d2245a39cbd0899 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Thu, 16 Apr 2020 17:51:49 +0200 Subject: [PATCH] tpl engine --- src/Controller/PollController.php | 9 ++++---- src/Service/MailService.php | 38 ++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 9c47dab..94d936f 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -139,7 +139,7 @@ class PollController extends FramadateController { * * @return JsonResponse */ - public function newPollAction( Request $request, Swift_Mailer $mailer, MailService $mail_service ) { + public function newPollAction( Request $request ) { $data = $request->getContent(); @@ -227,7 +227,7 @@ class PollController extends FramadateController { $precision = 'from an existing user : ' . $foundOwner->getEmail(); } - $mail_service->sendCreationMailAction( $foundOwner, $newpoll ); + $this->mail_service->sendCreationMailAction( $foundOwner, $newpoll ); return $this->json( [ 'message' => 'you created a poll ' . $precision, @@ -258,7 +258,6 @@ class PollController extends FramadateController { */ // public function sendCreationMailAction( Owner $admin_user, Poll $poll, \Swift_Mailer $mailer) { public function testSendCreationMailAction( - MailService $mail_service, $emailChoice = 'tktest_commentateur@tktest.com' ) { $em = $this->getDoctrine()->getRepository( Owner::class ); @@ -267,9 +266,9 @@ class PollController extends FramadateController { $poll = $foundOwner->getPolls()[ 0 ]; $comment = $foundOwner->getComments()[ 0 ]; - $sent = $mail_service->sendCreationMailAction( $foundOwner, $poll ); + $sent = $this->mail_service->sendCreationMailAction( $foundOwner, $poll ); if ( $sent ) { - return $this->json( [ "message" => "test email sent!" ], 200 ); + return $this->json( [ "message" => $this->mail_service->numSent. "test email sent to ".$foundOwner->getEmail()."!" ], 200 ); } } diff --git a/src/Service/MailService.php b/src/Service/MailService.php index fef8ae0..adb4a9a 100644 --- a/src/Service/MailService.php +++ b/src/Service/MailService.php @@ -12,6 +12,7 @@ use Exception; use Swift_Mailer; use Swift_Message; use Swift_SmtpTransport; +use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; class MailService { @@ -22,10 +23,23 @@ class MailService { private $em; private $mailer; + /** + * @var int + */ + private $numSent; // public function __construct( EntityManagerInterface $entityManager , Mailer $mailer) { - public function __construct( EntityManagerInterface $entityManager ) { - $this->em = $entityManager; + /** + * @var EngineInterface + */ + private $templating; + + public function __construct( + EntityManagerInterface $entityManager, + EngineInterface $templating + ) { + $this->em = $entityManager; + $this->templating = $templating; // Create the Transport $transport = new Swift_SmtpTransport( 'localhost', 25 ); @@ -150,14 +164,22 @@ class MailService { 'creation_vote' => 'Framadate | Vote de "' . $config[ 'owner' ]->getPseudo() . '" - sondage ' . $config[ 'poll' ]->getTitle(), ]; // Create a message - $message = ( new Swift_Message( 'Wonderful Subject' ) ) - ->setFrom( [ 'contact@framadate-api.cipherbliss.com' ] ) - ->setTo( ['contact@cipherbliss.com'] ) - ->setBody( 'Here is the message itself' ); + $htmlbody = $this->templating->render( + // templates/emails/registration.html.twig + $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->mailer->send( $message ); - printf( "Sent %d messages\n", $numSent ); + $numSent = $this->mailer->send( $message ); + $this->numSent = $numSent; // $email = ( new TemplatedEmail() ) // ->from( new Address( $config[ 'from' ] ) ) // ->subject( $config[ 'title' ] )