mirror of
https://framagit.org/tykayn/date-poll-api
synced 2023-08-25 08:23:11 +02:00
refacto generic email send
This commit is contained in:
parent
30318d17ab
commit
84f271ff5a
@ -261,36 +261,7 @@ class PollController extends AbstractController {
|
|||||||
$poll = $foundOwner->getPolls()[ 0 ];
|
$poll = $foundOwner->getPolls()[ 0 ];
|
||||||
$comment = $foundOwner->getComments()[ 0 ];
|
$comment = $foundOwner->getComments()[ 0 ];
|
||||||
|
|
||||||
|
$mail_service->sendCreationMailAction( $foundOwner, $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 - ' . $poll->getTitle(),
|
|
||||||
'edit_poll' => 'Framadate | Modification de sondage - ' . $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 '. $poll->getTitle(),
|
|
||||||
'creation_comment' => 'Framadate | Commentaire de "' . $foundOwner->getPseudo() . '" - sondage ' . $poll->getTitle(),
|
|
||||||
'creation_vote' => 'Framadate | Vote de "' . $foundOwner->getPseudo() . '" - sondage ' . $poll->getTitle(),
|
|
||||||
];
|
|
||||||
|
|
||||||
// $mail_service->sendCreationMailAction( $foundOwner, $poll );
|
|
||||||
$templateVars = [
|
|
||||||
'owner' => $foundOwner,
|
|
||||||
'comment' => $comment,
|
|
||||||
'poll' => $poll,
|
|
||||||
'email_template' => 'emails/' . $emailChoicesTemplates[ $emailChoice ],
|
|
||||||
'title' => $emailChoicesTitles[ $emailChoice ],
|
|
||||||
];
|
|
||||||
|
|
||||||
return $this->render( $templateVars[ 'email_template' ], $templateVars );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,13 +8,11 @@ use App\Entity\Owner;
|
|||||||
use App\Entity\Poll;
|
use App\Entity\Poll;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Swift_Mailer;
|
use http\Header;
|
||||||
use Symfony\Component\Mailer\Mailer;
|
|
||||||
use Symfony\Component\Mailer\MailerInterface;
|
|
||||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||||
|
use Symfony\Component\Mailer\Mailer;
|
||||||
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
|
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
|
||||||
use Symfony\Component\Mime\Address;
|
use Symfony\Component\Mime\Address;
|
||||||
use Symfony\Component\Mime\Email;
|
|
||||||
|
|
||||||
class MailService {
|
class MailService {
|
||||||
|
|
||||||
@ -28,53 +26,42 @@ class MailService {
|
|||||||
|
|
||||||
public function __construct( EntityManagerInterface $entityManager ) {
|
public function __construct( EntityManagerInterface $entityManager ) {
|
||||||
$this->em = $entityManager;
|
$this->em = $entityManager;
|
||||||
|
|
||||||
|
|
||||||
$transport = new EsmtpTransport();
|
$transport = new EsmtpTransport();
|
||||||
$mailer = new Mailer($transport);
|
$this->mailer = new Mailer( $transport );
|
||||||
$this->mailer = $mailer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, MailerInterface $mailer ) {
|
/**
|
||||||
$em = $this->em->getRepository( Owner::class );
|
* @param Owner $foundOwner
|
||||||
$admin_user = $foundOwner;
|
* @param Poll $poll
|
||||||
$poll = $newpoll;
|
* @param Mailer $mailer
|
||||||
|
*
|
||||||
|
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
||||||
|
*/
|
||||||
|
public function sendCreationMailAction( Owner $foundOwner, Poll $poll) {
|
||||||
|
|
||||||
// anti spam , limit to every minute TODO
|
// anti spam , limit to every minute TODO
|
||||||
|
|
||||||
$templateVars = [
|
$config = [
|
||||||
'owner' => $admin_user,
|
'owner' => $foundOwner,
|
||||||
|
'from' => 'ne-pas-repondre@framadate-api.cipherbliss.com',
|
||||||
'poll' => $poll,
|
'poll' => $poll,
|
||||||
'title' => 'Création de sondage - ' . $poll->getTitle(),
|
'title' => 'Création de sondage - ' . $poll->getTitle(),
|
||||||
'email_template' => 'emails/creation-mail.html.twig',
|
'email_template' => 'emails/creation-mail.html.twig',
|
||||||
];
|
];
|
||||||
|
|
||||||
$email = ( new Email( ) )
|
return $this->sendMailWithVars( $config );
|
||||||
->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
|
|
||||||
->setContentType( 'text/html' )
|
|
||||||
->setCharset( 'UTF-8' )
|
|
||||||
->subject('Framadate - mes sondages')
|
|
||||||
->setTo( $admin_user->getEmail() )
|
|
||||||
->htmlTemplate( $templateVars[ 'email_template' ] )
|
|
||||||
->context( $templateVars );
|
|
||||||
|
|
||||||
// send email
|
|
||||||
return $mailer->send( $email );
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* send created polls to an owner
|
|
||||||
*
|
|
||||||
* @param Owner $foundOwner
|
|
||||||
*
|
|
||||||
* @return int|void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function sendOwnerPollsAction( Owner $foundOwner ) {
|
|
||||||
|
|
||||||
// anti spam , limit to every minute TODO
|
/**
|
||||||
|
* anti spam , limit to every minute TODO
|
||||||
|
*
|
||||||
|
* @param Owner $owner
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function antispamCheck( Owner $owner ) {
|
||||||
|
|
||||||
// $lastSend = $admin_user->getRequestedPollsDate();
|
// $lastSend = $admin_user->getRequestedPollsDate();
|
||||||
// $now = new \DateTime();
|
// $now = new \DateTime();
|
||||||
|
|
||||||
@ -85,22 +72,66 @@ class MailService {
|
|||||||
// $admin_user->setRequestedPollsDate( $now );
|
// $admin_user->setRequestedPollsDate( $now );
|
||||||
// $em->persist( $admin_user );
|
// $em->persist( $admin_user );
|
||||||
// $em->flush();
|
// $em->flush();
|
||||||
$titleEmail = 'Framadate | Mes sondages';
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
$templateVars = [
|
/**
|
||||||
'owner' => $foundOwner,
|
* send created polls to an owner
|
||||||
'title' => $titleEmail,
|
*
|
||||||
|
* @param Owner $owner
|
||||||
|
*
|
||||||
|
* @return int|void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function sendOwnerPollsAction( Owner $owner ) {
|
||||||
|
|
||||||
|
$config = [
|
||||||
|
'owner' => $owner,
|
||||||
|
'title' => 'Framadate | Mes sondages',
|
||||||
'email_template' => 'emails/owner-list.html.twig',
|
'email_template' => 'emails/owner-list.html.twig',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
return $this->sendMailWithVars( $config );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* generic way to send email with html template
|
||||||
|
*
|
||||||
|
* @param $config
|
||||||
|
*
|
||||||
|
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
||||||
|
*/
|
||||||
|
public function sendMailWithVars( $config ) {
|
||||||
|
|
||||||
|
$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(),
|
||||||
|
];
|
||||||
|
|
||||||
$email = ( new TemplatedEmail() )
|
$email = ( new TemplatedEmail() )
|
||||||
->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
|
->from( new Address( $config[ 'from' ] ) )
|
||||||
->to( new Address( $foundOwner->getEmail() ) )
|
// ->setHeaders( [new Header('charset', 'UTF-8' )])
|
||||||
->subject( $titleEmail )
|
->subject( $config[ 'title' ] )
|
||||||
->htmlTemplate( 'emails/owner-list.html.twig')
|
->to( $config[ 'owner' ]->getEmail() )
|
||||||
->context( $templateVars );
|
->htmlTemplate( $config[ 'email_template' ] )
|
||||||
|
->context( $config );
|
||||||
|
|
||||||
// send email
|
// send email
|
||||||
return $this->mailer->send( $email );
|
return $this->
|
||||||
|
mailer->send( $email );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
templates/email-base-plaintext.html.twig
Normal file
15
templates/email-base-plaintext.html.twig
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{% block title %}
|
||||||
|
{% if title is defined %}
|
||||||
|
<h1>{{ title }}</h1>
|
||||||
|
{% else %}
|
||||||
|
<h1>Framadate - email</h1>
|
||||||
|
{% endif %}
|
||||||
|
<hr>
|
||||||
|
{% endblock %}
|
||||||
|
{% block stylesheets %}{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{% endblock %}
|
||||||
|
{% block footer %}
|
||||||
|
{% include 'emails/footer.html.twig' %}
|
||||||
|
{% endblock %}
|
Loading…
Reference in New Issue
Block a user