send html template

This commit is contained in:
tykayn 2020-04-14 18:09:03 +02:00
parent f0041c12af
commit 9b7f2ebd32
3 changed files with 72 additions and 25 deletions

2
.env
View File

@ -38,7 +38,7 @@ CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=MAILER_URL=sendmail://framadate-api.cipherbliss.com
MAILER_URL=sendmail://framadate-api.cipherbliss.com
# set the support email who will answer users in case of emergency
SUPPORT_EMAIL=admin_framadate@yopmail.com
###< symfony/swiftmailer-bundle ###

View File

@ -3,9 +3,9 @@
namespace App\Controller;
use App\Entity\Owner;
use App\Service\MailService;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
use Swift_Message;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
@ -15,7 +15,14 @@ use Symfony\Component\HttpFoundation\JsonResponse;
* @Route("/api/v1",name="api_")
*/
class DefaultController extends AbstractController {
/**
* @var MailService
*/
private $mail_service;
public function __construct(MailService $mail_service) {
$this->mail_service = $mail_service;
}
/**
* Send a mail with all the data to one user
@ -33,28 +40,20 @@ class DefaultController extends AbstractController {
$repository = $this->getDoctrine()->getRepository( Owner::class );
// find user by email
$founduser = $repository->findOneBy( [ 'email' => $email ] );
$founduser = $repository->findOneBy( [ 'email' => $email ] );
if ( $founduser ) {
$polls = $founduser->getPolls();
$templateVars = [
'owner' => $founduser,
'polls' => $polls,
'title' => 'Mes sondages - '.$email,
'title' => 'Mes sondages - ' . $email,
];
$message = ( new Swift_Message( 'Framadate - mes sondages' ) )
->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
->setTo( $founduser->getEmail() )
->setBody(
$this->renderView(
'emails/owner-list.html.twig',
$templateVars,
'text/html'
)
);
// send email
$mailer->send( $message );
$this->mail_service->sendOwnerPollsAction();
return $this->json( [
'message' => 'mail succefully sent to user ' . $email,
@ -63,8 +62,7 @@ class DefaultController extends AbstractController {
200 );
} else { // user not found case
} else { // user not found case
return $this->json( [
'message' => 'no user found for email ' . $email,
'data' => '',

View File

@ -9,12 +9,24 @@ use App\Entity\Poll;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Swift_Message;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Address;
class MailService {
public function __construct(EntityManagerInterface $entityManager) {
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var \Swift_Mailer
*/
private $mailer;
public function __construct(EntityManagerInterface $entityManager, \Swift_Mailer $mailer) {
$this->em = $entityManager;
$this->mailer = $mailer;
}
public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, \Swift_Mailer $mailer ) {
@ -47,16 +59,53 @@ class MailService {
->setContentType('text/html')
->setCharset('UTF-8')
->setTo( $admin_user->getEmail() )
->setBody(
$this->renderView(
$templateVars[ 'email_template' ],
$templateVars
),
'text/html'
);
->htmlTemplate($templateVars[ 'email_template' ])
->context( $templateVars);
// send email
return $mailer->send( $message );
}
/**
* send created polls to an owner
* @param Owner $foundOwner
*
* @return int
* @throws \Exception
*/
public function sendOwnerPollsAction( Owner $foundOwner ) {
$em = $this->em->getRepository( Owner::class );
$admin_user = $foundOwner;
// 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();
$titleEmail = 'Framadate | Mes sondages';
$templateVars = [
'owner' => $admin_user,
'title' => $titleEmail,
'email_template' => 'emails/owner-polls.html.twig',
];
$email = ( new TemplatedEmail( $titleEmail ) )
->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
->to( new Address($admin_user->getEmail() ) )
->htmlTemplate($templateVars[ 'email_template' ])
->context( $templateVars);
// send email
return $this->mailer->send( $email );
}
}