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

60 lines
1.3 KiB
PHP

<?php
namespace App\Service;
use App\Entity\Owner;
use App\Entity\Poll;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Swift_Message;
class MailService {
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;
// 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' )
->setTo( $admin_user->getEmail() )
->setBody(
$this->renderView(
$templateVars[ 'email_template' ],
$templateVars
)
);
// send email
return $mailer->send( $message );
}
}