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\EntityManager;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Swift_Message;
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
class MailService {
|
|
|
|
|
|
|
|
|
2020-04-12 17:35:57 +02:00
|
|
|
public function __construct(EntityManagerInterface $entityManager) {
|
|
|
|
$this->em = $entityManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, \Swift_Mailer $mailer ) {
|
|
|
|
$em = $this->em->getRepository( Owner::class );
|
|
|
|
$admin_user = $foundOwner;
|
|
|
|
$poll = $newpoll;
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
$templateVars = [
|
|
|
|
'owner' => $admin_user,
|
|
|
|
'poll' => $poll,
|
|
|
|
'title' => 'Création de sondage - ' . $poll->getTitle(),
|
|
|
|
'email_template' => 'emails/creation-mail.html.twig',
|
|
|
|
];
|
|
|
|
|
|
|
|
$message = ( new Swift_Message( 'Framadate - mes sondages' ) )
|
|
|
|
->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
|
2020-04-13 17:35:57 +02:00
|
|
|
->setContentType('text/html')
|
|
|
|
->setCharset('UTF-8')
|
2020-04-12 17:28:30 +02:00
|
|
|
->setTo( $admin_user->getEmail() )
|
|
|
|
->setBody(
|
|
|
|
$this->renderView(
|
|
|
|
$templateVars[ 'email_template' ],
|
|
|
|
$templateVars
|
2020-04-13 17:35:57 +02:00
|
|
|
),
|
|
|
|
'text/html'
|
2020-04-12 17:28:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// send email
|
|
|
|
return $mailer->send( $message );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|