2020-04-12 17:28:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
|
2020-04-16 17:11:01 +02:00
|
|
|
use App\Entity\Comment;
|
2020-04-12 17:28:30 +02:00
|
|
|
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;
|
2020-04-16 17:40:00 +02:00
|
|
|
use Swift_Mailer;
|
|
|
|
use Swift_Message;
|
|
|
|
use Swift_SmtpTransport;
|
2021-04-27 10:22:16 +02:00
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
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 17:51:49 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $numSent;
|
2020-04-14 18:09:03 +02:00
|
|
|
|
2020-04-16 17:22:33 +02:00
|
|
|
// public function __construct( EntityManagerInterface $entityManager , Mailer $mailer) {
|
2020-04-16 18:03:09 +02:00
|
|
|
|
2020-04-16 17:51:49 +02:00
|
|
|
private $templating;
|
|
|
|
|
|
|
|
public function __construct(
|
2020-04-16 18:03:09 +02:00
|
|
|
EntityManagerInterface $entityManager
|
2020-04-16 17:51:49 +02:00
|
|
|
) {
|
2020-04-16 18:03:09 +02:00
|
|
|
$this->em = $entityManager;
|
2020-04-16 17:40:00 +02:00
|
|
|
|
|
|
|
// Create the Transport
|
|
|
|
$transport = new Swift_SmtpTransport( 'localhost', 25 );
|
|
|
|
|
|
|
|
// Create the Mailer using your created Transport
|
|
|
|
$mailer = new Swift_Mailer( $transport );
|
|
|
|
|
|
|
|
$this->mailer = $mailer;
|
2020-04-12 17:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 16:35:06 +02:00
|
|
|
/**
|
|
|
|
* @param Owner $foundOwner
|
2020-04-16 16:43:30 +02:00
|
|
|
* @param Poll|null $poll
|
2020-04-16 16:35:06 +02:00
|
|
|
*
|
2021-04-27 10:22:16 +02:00
|
|
|
* @throws TransportExceptionInterface
|
2020-04-16 16:35:06 +02:00
|
|
|
*/
|
2020-04-16 17:22:33 +02:00
|
|
|
public function sendCreationMailAction( Owner $foundOwner, Poll $poll = null ) {
|
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
|
|
|
|
2020-04-16 16:35:06 +02:00
|
|
|
$config = [
|
|
|
|
'owner' => $foundOwner,
|
|
|
|
'from' => 'ne-pas-repondre@framadate-api.cipherbliss.com',
|
2020-04-12 17:28:30 +02:00
|
|
|
'poll' => $poll,
|
2020-04-16 17:22:33 +02:00
|
|
|
'title' => 'Création de sondage - ' . ( $poll ? $poll->getTitle() : $poll ),
|
2020-04-12 17:28:30 +02:00
|
|
|
'email_template' => 'emails/creation-mail.html.twig',
|
|
|
|
];
|
|
|
|
|
2020-04-16 16:35:06 +02:00
|
|
|
return $this->sendMailWithVars( $config );
|
2020-04-12 17:28:30 +02:00
|
|
|
}
|
2020-04-14 18:09:03 +02:00
|
|
|
|
2020-04-16 16:35:06 +02:00
|
|
|
/**
|
|
|
|
* generic way to send email with html template
|
|
|
|
*
|
|
|
|
* @param $config
|
|
|
|
*
|
2021-04-27 10:22:16 +02:00
|
|
|
* @throws TransportExceptionInterface
|
2020-04-16 16:35:06 +02:00
|
|
|
*/
|
|
|
|
public function sendMailWithVars( $config ) {
|
|
|
|
|
2020-04-16 17:22:33 +02:00
|
|
|
if ( ! $config[ 'poll' ] ) {
|
|
|
|
$config[ 'poll' ] = new Poll();
|
2020-04-16 16:43:30 +02:00
|
|
|
}
|
2020-04-16 16:35:06 +02:00
|
|
|
$emailChoicesTemplates = [
|
2020-04-16 17:22:33 +02:00
|
|
|
'creation_poll' => 'creation-mail.html.twig',
|
|
|
|
'edit_poll' => 'modification-notification-mail.html.twig',
|
|
|
|
'creation_poll_admin' => 'author-mail.html.twig',
|
|
|
|
'owner_list' => 'owner-list.html.twig',
|
|
|
|
'expiration' => 'expiration-mail.html.twig',
|
|
|
|
'creation_comment' => 'comment-notification.html.twig',
|
|
|
|
'creation_vote' => 'vote-notification.html.twig',
|
2020-04-16 16:35:06 +02:00
|
|
|
];
|
|
|
|
$emailChoicesTitles = [
|
2020-04-16 17:22:33 +02:00
|
|
|
'creation_poll' => 'Framadate | Création de sondage - lien public - ' . $config[ 'poll' ]->getTitle(),
|
|
|
|
'edit_poll' => 'Framadate | Modification de sondage - ' . $config[ 'poll' ]->getTitle(),
|
|
|
|
'creation_poll_admin' => 'Framadate | Création de sondage - lien admin - ',
|
|
|
|
'owner_list' => 'Framadate | Vos sondages créés',
|
|
|
|
'expiration' => 'Framadate | Notice d\'expiration du sondage ' . $config[ 'poll' ]->getTitle(),
|
|
|
|
'creation_comment' => 'Framadate | Commentaire de "' . $config[ 'owner' ]->getPseudo() . '" - sondage ' . $config[ 'poll' ]->getTitle(),
|
|
|
|
'creation_vote' => 'Framadate | Vote de "' . $config[ 'owner' ]->getPseudo() . '" - sondage ' . $config[ 'poll' ]->getTitle(),
|
2020-04-16 16:35:06 +02:00
|
|
|
];
|
2020-04-16 17:40:00 +02:00
|
|
|
// Create a message
|
2020-04-16 17:51:49 +02:00
|
|
|
$htmlbody = $this->templating->render(
|
|
|
|
// templates/emails/registration.html.twig
|
|
|
|
$config[ 'email_template' ],
|
|
|
|
$config
|
|
|
|
);
|
|
|
|
$message = ( new Swift_Message( 'Wonderful Subject from FAT computer' ) )
|
|
|
|
->setContentType( "text/html" )
|
|
|
|
->setCharset( 'UTF-8' )
|
|
|
|
->setFrom( [ 'contact@framadate-api.cipherbliss.com' ] )
|
|
|
|
->setTo( [ 'contact@cipherbliss.com' ] )
|
|
|
|
->setBody( $htmlbody, 'text/html' );
|
|
|
|
|
2020-04-16 17:40:00 +02:00
|
|
|
|
|
|
|
// Send the message
|
2020-04-16 17:51:49 +02:00
|
|
|
$numSent = $this->mailer->send( $message );
|
|
|
|
$this->numSent = $numSent;
|
2020-04-16 17:40:00 +02:00
|
|
|
// $email = ( new TemplatedEmail() )
|
|
|
|
// ->from( new Address( $config[ 'from' ] ) )
|
|
|
|
// ->subject( $config[ 'title' ] )
|
|
|
|
// ->to( $config[ 'owner' ]->getEmail() )
|
|
|
|
// ->htmlTemplate( $config[ 'email_template' ] )
|
|
|
|
// ->context( $config );
|
2020-04-16 17:22:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
// $email = ( new \Swift_Mime_SimpleMessage($config[ 'from' ]) )
|
|
|
|
// ->setFrom( new Address( $config[ 'from' ] ) )
|
2020-04-16 17:11:01 +02:00
|
|
|
//// ->setHeaders( [new Header('charset', 'UTF-8' )])
|
2020-04-16 17:22:33 +02:00
|
|
|
// ->setSubject( $config[ 'title' ] )
|
|
|
|
// ->setTo( $config[ 'owner' ]->getEmail() )
|
|
|
|
// ->setBody("ble blah bleuh <strong> strong text </strong> swift mailer","text/html");
|
|
|
|
|
|
|
|
// $email = (new TemplatedEmail())
|
|
|
|
// ->from('fabien@example.com')
|
|
|
|
// ->to(new Address('ryan@example.com'))
|
|
|
|
// ->subject('Thanks for signing up!')
|
|
|
|
//
|
|
|
|
// // path of the Twig template to render
|
|
|
|
// ->htmlTemplate('emails/footer.html.twig')
|
|
|
|
//
|
|
|
|
// // pass variables (name => value) to the template
|
|
|
|
// ->context([
|
|
|
|
// 'expiration_date' => new \DateTime('+7 days'),
|
|
|
|
// 'username' => 'foo',
|
|
|
|
// ])
|
|
|
|
// ;
|
2020-04-14 18:09:03 +02:00
|
|
|
|
|
|
|
// send email
|
2020-04-16 17:40:00 +02:00
|
|
|
|
|
|
|
|
2020-04-14 18:09:03 +02:00
|
|
|
}
|
2021-04-27 10:22:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* anti spam , limit to every minute TODO
|
|
|
|
*
|
|
|
|
* @param Owner $owner
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function antispamCheck( Owner $owner ) {
|
|
|
|
|
|
|
|
// $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();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* send created polls to an owner
|
|
|
|
*
|
|
|
|
* @param Owner $owner
|
|
|
|
*
|
|
|
|
* @return int|void
|
|
|
|
* @throws Exception
|
|
|
|
* @throws TransportExceptionInterface
|
|
|
|
*/
|
|
|
|
public function sendOwnerPollsAction( Owner $owner ) {
|
|
|
|
|
|
|
|
$config = [
|
|
|
|
'owner' => $owner,
|
|
|
|
'title' => 'Framadate | Mes sondages',
|
|
|
|
'email_template' => 'emails/owner-list.html.twig',
|
|
|
|
];
|
|
|
|
$this->sendMailWithVars( $config );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Comment $comment
|
|
|
|
*
|
|
|
|
* @throws TransportExceptionInterface
|
|
|
|
*/
|
|
|
|
public function sendCommentNotification( Comment $comment ) {
|
|
|
|
|
|
|
|
$config = [
|
|
|
|
'comment' => $comment,
|
|
|
|
'owner' => $comment->getOwner(),
|
|
|
|
'title' => 'Framadate | commentaire de ' . $comment->getOwner()->getPseudo() . ' _ sondage ' . $comment->getPoll()->getTitle(),
|
|
|
|
'email_template' => 'emails/comment-notification.html.twig',
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->sendMailWithVars( $config );
|
|
|
|
}
|
2020-04-12 17:28:30 +02:00
|
|
|
}
|