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 ];
|
||||
$comment = $foundOwner->getComments()[ 0 ];
|
||||
|
||||
|
||||
$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 );
|
||||
$mail_service->sendCreationMailAction( $foundOwner, $poll );
|
||||
}
|
||||
|
||||
|
||||
|
@ -8,13 +8,11 @@ use App\Entity\Owner;
|
||||
use App\Entity\Poll;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exception;
|
||||
use Swift_Mailer;
|
||||
use Symfony\Component\Mailer\Mailer;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
use http\Header;
|
||||
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 {
|
||||
|
||||
@ -28,53 +26,42 @@ class MailService {
|
||||
|
||||
public function __construct( EntityManagerInterface $entityManager ) {
|
||||
$this->em = $entityManager;
|
||||
|
||||
|
||||
$transport = new EsmtpTransport();
|
||||
$mailer = new Mailer($transport);
|
||||
$this->mailer = $mailer;
|
||||
$this->mailer = new Mailer( $transport );
|
||||
}
|
||||
|
||||
public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, MailerInterface $mailer ) {
|
||||
$em = $this->em->getRepository( Owner::class );
|
||||
$admin_user = $foundOwner;
|
||||
$poll = $newpoll;
|
||||
|
||||
/**
|
||||
* @param Owner $foundOwner
|
||||
* @param Poll $poll
|
||||
* @param Mailer $mailer
|
||||
*
|
||||
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
||||
*/
|
||||
public function sendCreationMailAction( Owner $foundOwner, Poll $poll) {
|
||||
|
||||
// anti spam , limit to every minute TODO
|
||||
|
||||
$templateVars = [
|
||||
'owner' => $admin_user,
|
||||
$config = [
|
||||
'owner' => $foundOwner,
|
||||
'from' => 'ne-pas-repondre@framadate-api.cipherbliss.com',
|
||||
'poll' => $poll,
|
||||
'title' => 'Création de sondage - ' . $poll->getTitle(),
|
||||
'email_template' => 'emails/creation-mail.html.twig',
|
||||
];
|
||||
|
||||
$email = ( new Email( ) )
|
||||
->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 );
|
||||
|
||||
return $this->sendMailWithVars( $config );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
// $now = new \DateTime();
|
||||
|
||||
@ -85,22 +72,66 @@ class MailService {
|
||||
// $admin_user->setRequestedPollsDate( $now );
|
||||
// $em->persist( $admin_user );
|
||||
// $em->flush();
|
||||
$titleEmail = 'Framadate | Mes sondages';
|
||||
return true;
|
||||
}
|
||||
|
||||
$templateVars = [
|
||||
'owner' => $foundOwner,
|
||||
'title' => $titleEmail,
|
||||
/**
|
||||
* send created polls to an owner
|
||||
*
|
||||
* @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',
|
||||
];
|
||||
|
||||
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() )
|
||||
->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
|
||||
->to( new Address( $foundOwner->getEmail() ) )
|
||||
->subject( $titleEmail )
|
||||
->htmlTemplate( 'emails/owner-list.html.twig')
|
||||
->context( $templateVars );
|
||||
->from( new Address( $config[ 'from' ] ) )
|
||||
// ->setHeaders( [new Header('charset', 'UTF-8' )])
|
||||
->subject( $config[ 'title' ] )
|
||||
->to( $config[ 'owner' ]->getEmail() )
|
||||
->htmlTemplate( $config[ 'email_template' ] )
|
||||
->context( $config );
|
||||
|
||||
// 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