1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00

use twig renderer

This commit is contained in:
Baptiste Lemoine 2020-04-16 18:03:09 +02:00
parent bfd938d190
commit fe59537bc6
3 changed files with 84 additions and 13 deletions

View File

@ -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;
}
}

View File

@ -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 );
}
}

View File

@ -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 );