try to send example template html

This commit is contained in:
Baptiste Lemoine 2020-04-16 17:11:01 +02:00
parent 8f8eda6109
commit bdd0325c82
15 changed files with 87 additions and 49 deletions

View File

@ -5,12 +5,12 @@ namespace App\Controller;
use App\Entity\Comment;
use App\Entity\Owner;
use App\Entity\Poll;
use DateTime;
use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\SerializerBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
@ -19,8 +19,7 @@ use Symfony\Component\HttpFoundation\Request;
* @package App\Controller
* @Route("/api/v1",name="api_")
*/
class CommentController extends AbstractController {
class CommentController extends FramadateController {
/**
* @Get(
@ -71,7 +70,7 @@ class CommentController extends AbstractController {
$foundOwner = new Owner();
$foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] )
->setEmail( $data[ 'owner' ][ 'email' ] )
->setModifierToken( uniqid() );
->setModifierToken( uniqid( '', true ) );
}
// anti flood
$seconds_limit_lastpost = 5;
@ -84,7 +83,7 @@ class CommentController extends AbstractController {
// check time of last comment
$now = new \DateTime();
$now = new DateTime();
$now = $now->format( 'Y-m-d H:i:s' );
$date_first = strtotime( $lastCommentOfOwner[ 0 ]->getCreatedAt()->format( 'Y-m-d H:i:s' ) );
$date_second = strtotime( $now );
@ -107,7 +106,7 @@ class CommentController extends AbstractController {
}
}
$comment->setOwner( $foundOwner )
->setCreatedAt( new \DateTime() )
->setCreatedAt( new DateTime() )
->setPoll( $poll );
$foundOwner->addComment( $comment );
@ -116,6 +115,8 @@ class CommentController extends AbstractController {
$em->persist( $comment );
$em->flush();
$this->mail_service->sendCommentNotification($comment);
return $this->json( [
'message' => 'you created a comment',
'data' => [

View File

@ -6,7 +6,7 @@ use App\Entity\Owner;
use App\Service\MailService;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Swift_Mailer;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
@ -14,15 +14,12 @@ use Symfony\Component\HttpFoundation\JsonResponse;
* @package App\Controller
* @Route("/api/v1",name="api_")
*/
class DefaultController extends AbstractController {
class DefaultController extends FramadateController {
/**
* @var MailService
*/
private $mail_service;
protected $mail_service;
public function __construct( MailService $mail_service ) {
$this->mail_service = $mail_service;
}
/**
* Send a mail with all the data to one user
@ -32,11 +29,11 @@ class DefaultController extends AbstractController {
* )
*
* @param $email
* @param \Swift_Mailer $mailer
* @param Swift_Mailer $mailer
*
* @return JsonResponse
*/
public function sendPollsToUser( $email, \Swift_Mailer $mailer ) {
public function sendPollsToUser( $email, Swift_Mailer $mailer ) {
$repository = $this->getDoctrine()->getRepository( Owner::class );
// find user by email

View File

@ -0,0 +1,12 @@
<?php
namespace App\Controller;
use App\Service\MailService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class FramadateController extends AbstractController {
public function __construct( MailService $mail_service ) {
$this->mail_service = $mail_service;
}
}

View File

@ -4,14 +4,13 @@ namespace App\Controller;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* Class DefaultController
* @package App\Controller
* @Route("/",name="homepage")
*/
class IndexController extends AbstractController {
class IndexController extends FramadateController {
/**
* @Get(path ="/",
* name = "get_default")

View File

@ -12,7 +12,7 @@ use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Put;
use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\SerializerBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Swift_Mailer;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
@ -21,7 +21,8 @@ use Symfony\Component\HttpFoundation\Request;
* @package App\Controller
* @Route("/api/v1/poll",name="api_")
*/
class PollController extends AbstractController {
class PollController extends FramadateController {
/**
* @Get(
@ -138,7 +139,7 @@ class PollController extends AbstractController {
*
* @return JsonResponse
*/
public function newPollAction( Request $request, \Swift_Mailer $mailer, MailService $mail_service ) {
public function newPollAction( Request $request, Swift_Mailer $mailer, MailService $mail_service ) {
$data = $request->getContent();
@ -250,24 +251,29 @@ class PollController extends AbstractController {
*
* @param Owner $admin_user
* @param Poll $poll
* @param \Swift_Mailer $mailer
* @param Swift_Mailer $mailer
*
* @return int
* not that the email tktest_commentateur@tktest.com does not really exist
*/
// public function sendCreationMailAction( Owner $admin_user, Poll $poll, \Swift_Mailer $mailer) {
public function testSendCreationMailAction( MailService $mail_service, $emailChoice = 'creation_vote' ) {
public function testSendCreationMailAction(
MailService $mail_service,
$emailChoice = 'tktest_commentateur@tktest.com'
) {
$em = $this->getDoctrine()->getRepository( Owner::class );
$foundOwner = $em->findOneByEmail( $emailChoice );
if($foundOwner){
$poll = $foundOwner->getPolls()[ 0 ];
$comment = $foundOwner->getComments()[ 0 ];
if ( $foundOwner ) {
$poll = $foundOwner->getPolls()[ 0 ];
$comment = $foundOwner->getComments()[ 0 ];
$sent = $mail_service->sendCreationMailAction( $foundOwner, $poll );
if($sent){
return $this->json(["message"=>"test email sent!"],200);
if ( $sent ) {
return $this->json( [ "message" => "test email sent!" ], 200 );
}
}
return $this->json(["message"=>"user with this email was not found"],400);
return $this->json( [ "message" => "user with this email was not found" ], 400 );
}
@ -332,6 +338,7 @@ class PollController extends AbstractController {
$em->flush();
return $this->json( [
'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ),
'data' => [

View File

@ -11,7 +11,6 @@ use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Patch;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
@ -20,7 +19,7 @@ use Symfony\Component\HttpFoundation\Request;
* @package App\Controller
* @Route("/api/v1",name="api_")
*/
class VoteController extends AbstractController {
class VoteController extends FramadateController {
/**
* add a vote stack on a poll

View File

@ -4,6 +4,7 @@
namespace App\Service;
use App\Entity\Comment;
use App\Entity\Owner;
use App\Entity\Poll;
use Doctrine\ORM\EntityManagerInterface;
@ -13,6 +14,7 @@ use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
class MailService {
@ -89,6 +91,19 @@ class MailService {
'title' => 'Framadate | Mes sondages',
'email_template' => 'emails/owner-list.html.twig',
];
$this->sendMailWithVars( $config );
return 1;
}
public function sendCommentNotification( Comment $comment ) {
$config = [
'comment' => $comment,
'owner' => $comment->getOwner(),
'title' => 'Framadate | commentaire de '.$comment->getOwner()->getPseudo(). ' _ sondage '. $comment->getPoll()->getTitle(),
'email_template' => 'emails/comment-notification.html.twig',
];
return $this->sendMailWithVars( $config );
}
@ -124,13 +139,27 @@ class MailService {
'creation_vote' => 'Framadate | Vote de "' . $config['owner']->getPseudo() . '" - sondage ' . $config['poll']->getTitle(),
];
$email = ( new TemplatedEmail() )
->from( new Address( $config[ 'from' ] ) )
// ->setHeaders( [new Header('charset', 'UTF-8' )])
->subject( $config[ 'title' ] )
->to( $config[ 'owner' ]->getEmail() )
->htmlTemplate( $config[ 'email_template' ] )
->context( $config );
// $email = ( new Email() )
// ->from( new Address( $config[ 'from' ] ) )
//// ->setHeaders( [new Header('charset', 'UTF-8' )])
// ->subject( $config[ 'title' ] )
// ->to( $config[ 'owner' ]->getEmail() )
// ->htmlTemplate( $config[ 'email_template' ] )
// ->context( $config );
$email = (new TemplatedEmail())
->from('fabien@example.com')
->to(new Address('ryan@example.com'))
->subject('Thanks for signing up!')
// path of the Twig template to render
->htmlTemplate('emails/footer.html.twig')
// pass variables (name => value) to the template
->context([
'expiration_date' => new \DateTime('+7 days'),
'username' => 'foo',
])
;
// send email
return $this->

View File

@ -1,5 +1,5 @@
{#[Framadate][Réservé à l'auteur] Sondage: TESSSSSSSSSST#}
{% extends 'email-base.html.twig' %}
{% extends 'email-base-plaintext.html.twig' %}
{% block content %}
<h1>

View File

@ -1,6 +0,0 @@
{#[Framadate] Notification d'un sondage : TESSSSSSSSSST#}
smoi vient de rédiger un commentaire.
Vous pouvez retrouver votre sondage avec le lien suivant : https://framadate.org/NGutN7jB9vtoGOEjCfUJWBwr/admin
Merci de votre confiance.
Framadate

View File

@ -1,5 +1,5 @@
{#[Framadate] Notification d'un sondage : TESSSSSSSSSST#}
{% extends 'email-base.html.twig' %}
{% extends 'email-base-plaintext.html.twig' %}
{% block content %}
<strong>
{{ owner.pseudo }}

View File

@ -1,5 +1,5 @@
{#[Framadate][Pour diffusion aux sondés] Sondage: TESSSSSSSSSST#}
{% extends 'email-base.html.twig' %}
{% extends 'email-base-plaintext.html.twig' %}
{% block content %}

View File

@ -1,5 +1,5 @@
{#[Framadate][expiration] Sondage: TESSSSSSSSSST#}
{% extends 'email-base.html.twig' %}
{% extends 'email-base-plaintext.html.twig' %}
{% block content %}

View File

@ -1,5 +1,5 @@
{#[Framadate] Participation au sondage : TESSSSSSSSSST#}
{% extends 'email-base.html.twig' %}
{% extends 'email-base-plaintext.html.twig' %}
{% block content %}
<strong>
{{ poll.owner.pseudo }} ,

View File

@ -1,5 +1,5 @@
{#[Framadate][Réservé à l'auteur] Sondage: TESSSSSSSSSST#}
{% extends 'email-base.html.twig' %}
{% extends 'email-base-plaintext.html.twig' %}
{% block content %}
<h2>

View File

@ -1,5 +1,5 @@
{#[Framadate] Notification d'un sondage : TESSSSSSSSSST#}
{% extends 'email-base.html.twig' %}
{% extends 'email-base-plaintext.html.twig' %}
{% block content %}
<strong>
{{ owner.pseudo }}