testing stuff

This commit is contained in:
Baptiste Lemoine 2020-04-16 17:40:00 +02:00
parent e235986483
commit 79c4bd2519
1 changed files with 35 additions and 18 deletions

View File

@ -9,10 +9,9 @@ use App\Entity\Owner;
use App\Entity\Poll;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Address;
use Swift_Mailer;
use Swift_Message;
use Swift_SmtpTransport;
class MailService {
@ -26,10 +25,15 @@ class MailService {
// public function __construct( EntityManagerInterface $entityManager , Mailer $mailer) {
public function __construct( EntityManagerInterface $entityManager ) {
$this->em = $entityManager;
$transport = new EsmtpTransport();
$this->mailer = new Mailer( $transport );
// $this->mailer = $mailer;
$this->em = $entityManager;
// Create the Transport
$transport = new Swift_SmtpTransport( 'localhost', 25 );
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer( $transport );
$this->mailer = $mailer;
}
/**
@ -83,6 +87,7 @@ class MailService {
*
* @return int|void
* @throws Exception
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/
public function sendOwnerPollsAction( Owner $owner ) {
@ -97,6 +102,11 @@ class MailService {
}
/**
* @param Comment $comment
*
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/
public function sendCommentNotification( Comment $comment ) {
$config = [
@ -106,7 +116,7 @@ class MailService {
'email_template' => 'emails/comment-notification.html.twig',
];
return $this->sendMailWithVars( $config );
$this->sendMailWithVars( $config );
}
/**
@ -139,14 +149,21 @@ class MailService {
'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
$message = ( new Swift_Message( 'Wonderful Subject' ) )
->setFrom( [ 'contact@framadate-api.cipherbliss.com' ] )
->setTo( ['contact@cipherbliss.com'] )
->setBody( 'Here is the message itself' );
$email = ( new TemplatedEmail() )
->from( new Address( $config[ 'from' ] ) )
// ->setHeaders( [new Header('charset', 'UTF-8' )])
->subject( $config[ 'title' ] )
->to( $config[ 'owner' ]->getEmail() )
->htmlTemplate( $config[ 'email_template' ] )
->context( $config );
// Send the message
$numSent = $this->mailer->send( $message );
printf( "Sent %d messages\n", $numSent );
// $email = ( new TemplatedEmail() )
// ->from( new Address( $config[ 'from' ] ) )
// ->subject( $config[ 'title' ] )
// ->to( $config[ 'owner' ]->getEmail() )
// ->htmlTemplate( $config[ 'email_template' ] )
// ->context( $config );
// $email = ( new \Swift_Mime_SimpleMessage($config[ 'from' ]) )
@ -172,7 +189,7 @@ class MailService {
// ;
// send email
return $this->
mailer->send( $email );
}
}