date-poll-api/src/Service/MailService.php

107 lines
2.5 KiB
PHP
Raw Normal View History

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;
use Swift_Mailer;
2020-04-16 11:24:56 +02:00
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;
2020-04-14 18:09:03 +02:00
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
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-14 18:39:29 +02:00
use Symfony\Component\Mime\Email;
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 11:24:56 +02:00
$transport = new EsmtpTransport();
$mailer = new Mailer($transport);
2020-04-14 18:09:03 +02:00
$this->mailer = $mailer;
2020-04-12 17:35:57 +02:00
}
2020-04-16 11:24:56 +02:00
public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, MailerInterface $mailer ) {
2020-04-12 17:35:57 +02:00
$em = $this->em->getRepository( Owner::class );
$admin_user = $foundOwner;
$poll = $newpoll;
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
$templateVars = [
'owner' => $admin_user,
'poll' => $poll,
'title' => 'Création de sondage - ' . $poll->getTitle(),
'email_template' => 'emails/creation-mail.html.twig',
];
2020-04-14 18:39:29 +02:00
$email = ( new Email( ) )
2020-04-12 17:28:30 +02:00
->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
2020-04-14 18:16:09 +02:00
->setContentType( 'text/html' )
->setCharset( 'UTF-8' )
2020-04-14 18:39:29 +02:00
->subject('Framadate - mes sondages')
2020-04-12 17:28:30 +02:00
->setTo( $admin_user->getEmail() )
2020-04-14 18:16:09 +02:00
->htmlTemplate( $templateVars[ 'email_template' ] )
->context( $templateVars );
2020-04-12 17:28:30 +02:00
// send email
2020-04-14 18:39:29 +02:00
return $mailer->send( $email );
2020-04-12 17:28:30 +02:00
}
2020-04-14 18:09:03 +02:00
/**
* send created polls to an owner
2020-04-14 18:16:09 +02:00
*
2020-04-14 18:09:03 +02:00
* @param Owner $foundOwner
*
* @return int
2020-04-14 18:39:29 +02:00
* @throws Exception
2020-04-14 18:09:03 +02:00
*/
public function sendOwnerPollsAction( Owner $foundOwner ) {
2020-04-14 18:16:09 +02:00
// anti spam , limit to every minute TODO
// $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-14 18:09:03 +02:00
$titleEmail = 'Framadate | Mes sondages';
$templateVars = [
2020-04-16 11:24:56 +02:00
'owner' => $foundOwner,
2020-04-14 18:09:03 +02:00
'title' => $titleEmail,
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-14 18:16:09 +02:00
$email = ( new TemplatedEmail() )
2020-04-14 18:09:03 +02:00
->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
2020-04-16 11:24:56 +02:00
->to( new Address( $foundOwner->getEmail() ) )
2020-04-14 18:16:09 +02:00
->subject( $titleEmail )
->htmlTemplate( $templateVars[ 'email_template' ] )
->context( $templateVars );
2020-04-14 18:09:03 +02:00
// send email
return $this->mailer->send( $email );
}
2020-04-12 17:28:30 +02:00
}