2020-04-12 17:28:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Entity\Owner;
|
2020-04-12 17:35:57 +02:00
|
|
|
use App\Entity\Poll;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-04-14 18:39:29 +02:00
|
|
|
use Exception;
|
2020-04-16 16:35:06 +02:00
|
|
|
use http\Header;
|
2020-04-14 18:09:03 +02:00
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
2020-04-16 16:35:06 +02:00
|
|
|
use Symfony\Component\Mailer\Mailer;
|
2020-04-16 11:24:56 +02:00
|
|
|
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
|
2020-04-14 18:09:03 +02:00
|
|
|
use Symfony\Component\Mime\Address;
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
class MailService {
|
|
|
|
|
|
|
|
|
2020-04-14 18:09:03 +02:00
|
|
|
/**
|
|
|
|
* @var EntityManagerInterface
|
|
|
|
*/
|
|
|
|
private $em;
|
2020-04-16 11:24:56 +02:00
|
|
|
|
2020-04-14 18:09:03 +02:00
|
|
|
private $mailer;
|
|
|
|
|
2020-04-16 11:24:56 +02:00
|
|
|
public function __construct( EntityManagerInterface $entityManager ) {
|
2020-04-14 18:16:09 +02:00
|
|
|
$this->em = $entityManager;
|
2020-04-16 16:35:06 +02:00
|
|
|
$transport = new EsmtpTransport();
|
|
|
|
$this->mailer = new Mailer( $transport );
|
2020-04-12 17:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 16:35:06 +02:00
|
|
|
/**
|
|
|
|
* @param Owner $foundOwner
|
|
|
|
* @param Poll $poll
|
|
|
|
* @param Mailer $mailer
|
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
|
|
|
*/
|
|
|
|
public function sendCreationMailAction( Owner $foundOwner, Poll $poll) {
|
2020-04-12 17:28:30 +02:00
|
|
|
|
2020-04-14 18:16:09 +02:00
|
|
|
// anti spam , limit to every minute TODO
|
2020-04-12 17:28:30 +02:00
|
|
|
|
2020-04-16 16:35:06 +02:00
|
|
|
$config = [
|
|
|
|
'owner' => $foundOwner,
|
|
|
|
'from' => 'ne-pas-repondre@framadate-api.cipherbliss.com',
|
2020-04-12 17:28:30 +02:00
|
|
|
'poll' => $poll,
|
|
|
|
'title' => 'Création de sondage - ' . $poll->getTitle(),
|
|
|
|
'email_template' => 'emails/creation-mail.html.twig',
|
|
|
|
];
|
|
|
|
|
2020-04-16 16:35:06 +02:00
|
|
|
return $this->sendMailWithVars( $config );
|
2020-04-12 17:28:30 +02:00
|
|
|
}
|
2020-04-14 18:09:03 +02:00
|
|
|
|
2020-04-16 16:35:06 +02:00
|
|
|
|
2020-04-14 18:09:03 +02:00
|
|
|
/**
|
2020-04-16 16:35:06 +02:00
|
|
|
* anti spam , limit to every minute TODO
|
2020-04-14 18:16:09 +02:00
|
|
|
*
|
2020-04-16 16:35:06 +02:00
|
|
|
* @param Owner $owner
|
2020-04-14 18:09:03 +02:00
|
|
|
*
|
2020-04-16 16:35:06 +02:00
|
|
|
* @return bool
|
2020-04-14 18:09:03 +02:00
|
|
|
*/
|
2020-04-16 16:35:06 +02:00
|
|
|
public function antispamCheck( Owner $owner ) {
|
2020-04-14 18:09:03 +02:00
|
|
|
|
2020-04-14 18:16:09 +02:00
|
|
|
// $lastSend = $admin_user->getRequestedPollsDate();
|
|
|
|
// $now = new \DateTime();
|
2020-04-14 18:09:03 +02:00
|
|
|
|
2020-04-14 18:16:09 +02:00
|
|
|
// if ( date_diff( $lastSend, $now ) < 60 ) {
|
|
|
|
// // too soon!
|
|
|
|
// die( 'too soon!' );
|
|
|
|
// }
|
|
|
|
// $admin_user->setRequestedPollsDate( $now );
|
|
|
|
// $em->persist( $admin_user );
|
|
|
|
// $em->flush();
|
2020-04-16 16:35:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-14 18:09:03 +02:00
|
|
|
|
2020-04-16 16:35:06 +02:00
|
|
|
/**
|
|
|
|
* 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',
|
2020-04-14 18:39:29 +02:00
|
|
|
'email_template' => 'emails/owner-list.html.twig',
|
2020-04-14 18:09:03 +02:00
|
|
|
];
|
|
|
|
|
2020-04-16 16:35:06 +02:00
|
|
|
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(),
|
|
|
|
];
|
|
|
|
|
2020-04-14 18:16:09 +02:00
|
|
|
$email = ( new TemplatedEmail() )
|
2020-04-16 16:35:06 +02:00
|
|
|
->from( new Address( $config[ 'from' ] ) )
|
|
|
|
// ->setHeaders( [new Header('charset', 'UTF-8' )])
|
|
|
|
->subject( $config[ 'title' ] )
|
|
|
|
->to( $config[ 'owner' ]->getEmail() )
|
|
|
|
->htmlTemplate( $config[ 'email_template' ] )
|
|
|
|
->context( $config );
|
2020-04-14 18:09:03 +02:00
|
|
|
|
|
|
|
// send email
|
2020-04-16 16:35:06 +02:00
|
|
|
return $this->
|
|
|
|
mailer->send( $email );
|
2020-04-14 18:09:03 +02:00
|
|
|
}
|
2020-04-12 17:28:30 +02:00
|
|
|
}
|