2020-04-16 17:11:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2020-04-16 18:03:09 +02:00
|
|
|
use App\Entity\Owner;
|
|
|
|
use App\Entity\Poll;
|
|
|
|
use JMS\Serializer\Type\Exception\Exception;
|
2021-04-27 10:22:16 +02:00
|
|
|
use Swift_Mailer;
|
2020-04-16 18:03:09 +02:00
|
|
|
use Swift_Message;
|
2020-04-16 17:11:01 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2021-04-27 10:22:16 +02:00
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
2020-04-16 17:11:01 +02:00
|
|
|
|
2021-02-24 10:30:00 +01:00
|
|
|
/**
|
|
|
|
* sending emails controller
|
|
|
|
*
|
2021-04-21 11:02:24 +02:00
|
|
|
* Class EmailsController
|
2021-02-24 10:30:00 +01:00
|
|
|
* @package App\Controller
|
|
|
|
*/
|
2021-04-21 11:02:24 +02:00
|
|
|
class EmailsController extends AbstractController {
|
2020-04-16 18:03:09 +02:00
|
|
|
|
2020-04-17 12:04:37 +02:00
|
|
|
private $mail_service;
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
public function __construct( Swift_Mailer $mailer ) {
|
2020-04-16 18:03:09 +02:00
|
|
|
$this->mail_service = $mailer;
|
|
|
|
}
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
/**
|
|
|
|
* 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' => $this->getParameter( 'WEBSITE_NAME' ) . ' | Mes sondages',
|
|
|
|
'email_template' => 'emails/owner-list.html.twig',
|
|
|
|
];
|
|
|
|
$this->sendMailWithVars( $config );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:03:09 +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 18:03:09 +02:00
|
|
|
*/
|
|
|
|
public function sendMailWithVars( $config ) {
|
|
|
|
|
|
|
|
if ( ! isset( $config[ 'poll' ] ) ) {
|
|
|
|
$config[ 'poll' ] = new Poll();
|
|
|
|
}
|
|
|
|
$emailChoicesTemplates = [
|
|
|
|
'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',
|
|
|
|
];
|
|
|
|
$emailChoicesTitles = [
|
|
|
|
'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(),
|
|
|
|
];
|
|
|
|
// Create a message
|
|
|
|
$htmlbody = $this->renderView(
|
|
|
|
$config[ 'email_template' ],
|
|
|
|
$config
|
|
|
|
);
|
2021-04-27 10:22:16 +02:00
|
|
|
$message = ( new Swift_Message( $config[ 'title' ] ) )
|
2020-04-16 18:03:09 +02:00
|
|
|
->setContentType( "text/html" )
|
|
|
|
->setCharset( 'UTF-8' )
|
2020-04-16 18:16:55 +02:00
|
|
|
->setFrom( [ 'ne-pas-repondre@framadate-api.cipherbliss.com' ] )
|
|
|
|
->setTo( [ $config[ 'owner' ]->getEmail() ] )
|
2020-04-16 18:03:09 +02:00
|
|
|
->setBody( $htmlbody, 'text/html' );
|
|
|
|
|
|
|
|
|
|
|
|
// Send the message
|
|
|
|
$numSent = $this->mail_service->send( $message );
|
|
|
|
$this->numSent = $numSent;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:06:43 +02:00
|
|
|
/**
|
|
|
|
* @param Owner $foundOwner
|
|
|
|
* @param Poll|null $poll
|
|
|
|
*
|
2021-04-27 10:22:16 +02:00
|
|
|
* @throws TransportExceptionInterface
|
2020-04-16 18:06:43 +02:00
|
|
|
*/
|
|
|
|
public function sendCreationMailAction( Owner $foundOwner, Poll $poll = null ) {
|
|
|
|
|
|
|
|
// anti spam , limit to every minute TODO
|
|
|
|
|
|
|
|
$config = [
|
|
|
|
'owner' => $foundOwner,
|
|
|
|
'from' => 'ne-pas-repondre@framadate-api.cipherbliss.com',
|
|
|
|
'poll' => $poll,
|
|
|
|
'title' => 'Création de sondage - ' . ( $poll ? $poll->getTitle() : $poll ),
|
|
|
|
'email_template' => 'emails/creation-mail.html.twig',
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendMailWithVars( $config );
|
|
|
|
}
|
2020-04-16 18:14:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Owner $owner
|
|
|
|
* @param $comment
|
|
|
|
*
|
|
|
|
* @return int
|
2021-04-27 10:22:16 +02:00
|
|
|
* @throws TransportExceptionInterface
|
2020-04-16 18:14:59 +02:00
|
|
|
*/
|
|
|
|
public function sendCommentNotificationAction( Owner $owner, $comment ) {
|
|
|
|
|
|
|
|
$config = [
|
|
|
|
'owner' => $owner,
|
2021-04-27 10:22:16 +02:00
|
|
|
'comment' => $comment,
|
|
|
|
'poll' => $comment->getPoll(),
|
2020-04-16 18:14:59 +02:00
|
|
|
'title' => 'Framadate | Commentaire de "' . $owner->getPseudo() . '" - sondage ' . $comment->getPoll()->getTitle(),
|
|
|
|
'email_template' => 'emails/comment-notification.html.twig',
|
|
|
|
];
|
|
|
|
$this->sendMailWithVars( $config );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Owner $owner
|
|
|
|
* @param $stackOfVotes
|
|
|
|
*
|
|
|
|
* @return int
|
2021-04-27 10:22:16 +02:00
|
|
|
* @throws TransportExceptionInterface
|
2020-04-16 18:14:59 +02:00
|
|
|
*/
|
|
|
|
public function sendVoteNotificationAction( Owner $owner, $stackOfVotes ) {
|
|
|
|
|
|
|
|
$config = [
|
|
|
|
'owner' => $owner,
|
2021-04-27 10:22:16 +02:00
|
|
|
'comment' => $stackOfVotes,
|
|
|
|
'poll' => $stackOfVotes->getPoll(),
|
2020-04-16 18:14:59 +02:00
|
|
|
'title' => 'Framadate | Vote de "' . $owner->getPseudo() . '" - sondage ' . $stackOfVotes->getPoll()->getTitle(),
|
|
|
|
'email_template' => 'emails/vote-notification.html.twig',
|
|
|
|
];
|
|
|
|
$this->sendMailWithVars( $config );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2020-04-16 17:11:01 +02:00
|
|
|
}
|