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

53 lines
1.2 KiB
PHP
Raw Normal View History

2020-04-12 17:28:30 +02:00
<?php
namespace App\Service;
use App\Entity\Owner;
class MailService {
public function sendCreationMailAction( $foundOwner, $newpoll, \Swift_Mailer $mailer ) {
$em = $this->getDoctrine()->getRepository( Owner::class );
$admin_user = $em->find( 1 );
$poll = $admin_user->getPolls()[ 0 ];
// 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,
'url' => $poll->getCustomUrl(),
'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 );
}
}