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

107 lines
2.5 KiB
PHP

<?php
namespace App\Service;
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 Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
class MailService {
/**
* @var EntityManagerInterface
*/
private $em;
private $mailer;
public function __construct( EntityManagerInterface $entityManager ) {
$this->em = $entityManager;
$transport = new EsmtpTransport();
$mailer = new Mailer($transport);
$this->mailer = $mailer;
}
public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, MailerInterface $mailer ) {
$em = $this->em->getRepository( Owner::class );
$admin_user = $foundOwner;
$poll = $newpoll;
// anti spam , limit to every minute TODO
$templateVars = [
'owner' => $admin_user,
'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 );
}
/**
* send created polls to an owner
*
* @param Owner $foundOwner
*
* @return int
* @throws Exception
*/
public function sendOwnerPollsAction( Owner $foundOwner ) {
// anti spam , limit to every minute TODO
// $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();
$titleEmail = 'Framadate | Mes sondages';
$templateVars = [
'owner' => $foundOwner,
'title' => $titleEmail,
'email_template' => 'emails/owner-list.html.twig',
];
$email = ( new TemplatedEmail() )
->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
->to( new Address( $foundOwner->getEmail() ) )
->subject( $titleEmail )
->htmlTemplate( $templateVars[ 'email_template' ] )
->context( $templateVars );
// send email
return $this->mailer->send( $email );
}
}