src/Service/MailerService.php line 104

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: margaux
  5.  * Date: 30/05/2018
  6.  * Time: 10:42
  7.  */
  8. namespace App\Service;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Swift_Message;
  11. use Spipu\Html2Pdf\Html2Pdf;
  12. use Mailjet\Resources;
  13. use Mailjet\Client;
  14. use Twig\Environment;
  15. class MailerService
  16. {
  17.     protected $mailer;
  18.     protected $templating;
  19.     protected $translator;
  20.     protected $mmr;
  21.     protected $prepo;
  22.     protected $params;
  23.     protected $historique;
  24.     private $em;
  25.     public function __construct(\Swift_Mailer $mailerEnvironment $templating,ParameterBagInterface $params)
  26.     {
  27.         $this->mailer $mailer;
  28.         $this->templating $templating;
  29.         $this->params $params;
  30.     }
  31.     public function sendHTMLEmailWithTemplate($from$to$subject$bodyParams$template$replyTo null$files = []){
  32.         $mj = new Client($this->params->get('user_mail_jet'), $this->params->get('key_mail_jet'),true,['version' => 'v3.1']);
  33.         $body = [
  34.             'Messages' => [
  35.                 [
  36.                     'From' => [
  37.                         'Email' => $from,
  38.                         'Name' => "SYSTEO - TimeSheet"
  39.                     ],
  40.                     'To' => [
  41.                         [
  42.                             'Email' => $to,
  43.                             'Name' => ""
  44.                         ]
  45.                     ],
  46.                     'Subject' => $subject,
  47.                     'TextPart' => Strip_tags($this->templating->render($template, array('content' => $bodyParams))),
  48.                     'HTMLPart' => $this->templating->render($template, array('content' => $bodyParams))
  49.                 ]
  50.             ]
  51.         ];
  52.         $response $mj->post(Resources::$Email, ['body' => $body]);
  53.         $response->success();
  54.     }
  55.     public function sendMultipleReceiversMultiplesMessages($subject,$template,$tab){
  56.         $mj = new Client($this->params->get('user_mail_jet'), $this->params->get('key_mail_jet'),true,['version' => 'v3.1']);
  57.         $messages = [];
  58.         foreach($tab as $value){
  59.             $messages[] = [
  60.                 'From' => [
  61.                     'Email' => $this->params->get('sender_address'),
  62.                     'Name' => "SYSTEO - timeSheet"
  63.                 ],
  64.                 'To' => [
  65.                     [
  66.                         'Email' => $value['email'],
  67.                         'Name' => $value['prenomNom']
  68.                     ]
  69.                 ],
  70.                 'Subject' => $subject,
  71.                 'TextPart' => Strip_tags($this->templating->render($template, array('content' => $value['content']))),
  72.                 'HTMLPart' => $this->templating->render($template, array('content' => $value['content']))
  73.             ];
  74.         }
  75.         $body = [
  76.             'Messages' => $messages
  77.         ];
  78.         $response $mj->post(Resources::$Email, ['body' => $body]);
  79.         $response->success() && var_dump($response->getData());
  80.     }
  81.     /*public function sendHTMLEmailWithTemplate($from, $to, $subject, $bodyParams, $template, $replyTo = null, $files = [])
  82.     {
  83.         $mail = new Swift_Message();
  84.         $mail
  85.             ->setFrom($from)
  86.             ->setTo($to)
  87.             ->setCharset('UTF-8')
  88.             ->setSubject($subject)
  89.             ->setBody(
  90.                 $this->templating->render($template, array('content' => $bodyParams)))
  91.             ->setContentType('text/html');
  92.         /*foreach($files as $f){
  93.             $mail->attach(\Swift_Attachment::fromPath($f));
  94.         }
  95.         if (!is_null($replyTo)) {
  96.             $mail->setReplyTo($replyTo);
  97.         }
  98.         try {
  99.             $this->mailer->send($mail);
  100.             return true;
  101.         } catch (\Exception $e) {
  102.             return false;
  103.         }
  104.     }*/
  105. }