mirror of
https://framagit.org/tykayn/date-poll-api
synced 2023-08-25 08:23:11 +02:00
send html template
This commit is contained in:
parent
f0041c12af
commit
9b7f2ebd32
2
.env
2
.env
@ -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 Gmail as a transport, use: "gmail://username:password@localhost"
|
||||||
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
|
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
|
||||||
# Delivery is disabled by default via "null://localhost"
|
# 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
|
# set the support email who will answer users in case of emergency
|
||||||
SUPPORT_EMAIL=admin_framadate@yopmail.com
|
SUPPORT_EMAIL=admin_framadate@yopmail.com
|
||||||
###< symfony/swiftmailer-bundle ###
|
###< symfony/swiftmailer-bundle ###
|
||||||
|
@ -3,9 +3,9 @@
|
|||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Entity\Owner;
|
use App\Entity\Owner;
|
||||||
|
use App\Service\MailService;
|
||||||
use FOS\RestBundle\Controller\Annotations\Get;
|
use FOS\RestBundle\Controller\Annotations\Get;
|
||||||
use FOS\RestBundle\Controller\Annotations\Route;
|
use FOS\RestBundle\Controller\Annotations\Route;
|
||||||
use Swift_Message;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
|
||||||
@ -15,7 +15,14 @@ use Symfony\Component\HttpFoundation\JsonResponse;
|
|||||||
* @Route("/api/v1",name="api_")
|
* @Route("/api/v1",name="api_")
|
||||||
*/
|
*/
|
||||||
class DefaultController extends AbstractController {
|
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
|
* Send a mail with all the data to one user
|
||||||
@ -33,28 +40,20 @@ class DefaultController extends AbstractController {
|
|||||||
$repository = $this->getDoctrine()->getRepository( Owner::class );
|
$repository = $this->getDoctrine()->getRepository( Owner::class );
|
||||||
|
|
||||||
// find user by email
|
// find user by email
|
||||||
$founduser = $repository->findOneBy( [ 'email' => $email ] );
|
$founduser = $repository->findOneBy( [ 'email' => $email ] );
|
||||||
|
|
||||||
if ( $founduser ) {
|
if ( $founduser ) {
|
||||||
$polls = $founduser->getPolls();
|
$polls = $founduser->getPolls();
|
||||||
$templateVars = [
|
$templateVars = [
|
||||||
'owner' => $founduser,
|
'owner' => $founduser,
|
||||||
'polls' => $polls,
|
'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
|
// send email
|
||||||
$mailer->send( $message );
|
$this->mail_service->sendOwnerPollsAction();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return $this->json( [
|
return $this->json( [
|
||||||
'message' => 'mail succefully sent to user ' . $email,
|
'message' => 'mail succefully sent to user ' . $email,
|
||||||
@ -63,8 +62,7 @@ class DefaultController extends AbstractController {
|
|||||||
200 );
|
200 );
|
||||||
|
|
||||||
|
|
||||||
|
} else { // user not found case
|
||||||
} else { // user not found case
|
|
||||||
return $this->json( [
|
return $this->json( [
|
||||||
'message' => 'no user found for email ' . $email,
|
'message' => 'no user found for email ' . $email,
|
||||||
'data' => '',
|
'data' => '',
|
||||||
|
@ -9,12 +9,24 @@ use App\Entity\Poll;
|
|||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Swift_Message;
|
use Swift_Message;
|
||||||
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||||
|
use Symfony\Component\Mime\Address;
|
||||||
|
|
||||||
class MailService {
|
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->em = $entityManager;
|
||||||
|
$this->mailer = $mailer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, \Swift_Mailer $mailer ) {
|
public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, \Swift_Mailer $mailer ) {
|
||||||
@ -47,16 +59,53 @@ class MailService {
|
|||||||
->setContentType('text/html')
|
->setContentType('text/html')
|
||||||
->setCharset('UTF-8')
|
->setCharset('UTF-8')
|
||||||
->setTo( $admin_user->getEmail() )
|
->setTo( $admin_user->getEmail() )
|
||||||
->setBody(
|
->htmlTemplate($templateVars[ 'email_template' ])
|
||||||
$this->renderView(
|
->context( $templateVars);
|
||||||
$templateVars[ 'email_template' ],
|
|
||||||
$templateVars
|
|
||||||
),
|
|
||||||
'text/html'
|
|
||||||
);
|
|
||||||
|
|
||||||
// send email
|
// send email
|
||||||
return $mailer->send( $message );
|
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 );
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user