<?php
/**
* Created by PhpStorm.
* User: margaux
* Date: 30/05/2018
* Time: 10:42
*/
namespace App\Service;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Swift_Message;
use Spipu\Html2Pdf\Html2Pdf;
use Mailjet\Resources;
use Mailjet\Client;
use Twig\Environment;
class MailerService
{
protected $mailer;
protected $templating;
protected $translator;
protected $mmr;
protected $prepo;
protected $params;
protected $historique;
private $em;
public function __construct(\Swift_Mailer $mailer, Environment $templating,ParameterBagInterface $params)
{
$this->mailer = $mailer;
$this->templating = $templating;
$this->params = $params;
}
public function sendHTMLEmailWithTemplate($from, $to, $subject, $bodyParams, $template, $replyTo = null, $files = []){
$mj = new Client($this->params->get('user_mail_jet'), $this->params->get('key_mail_jet'),true,['version' => 'v3.1']);
$body = [
'Messages' => [
[
'From' => [
'Email' => $from,
'Name' => "SYSTEO - TimeSheet"
],
'To' => [
[
'Email' => $to,
'Name' => ""
]
],
'Subject' => $subject,
'TextPart' => Strip_tags($this->templating->render($template, array('content' => $bodyParams))),
'HTMLPart' => $this->templating->render($template, array('content' => $bodyParams))
]
]
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
$response->success();
}
public function sendMultipleReceiversMultiplesMessages($subject,$template,$tab){
$mj = new Client($this->params->get('user_mail_jet'), $this->params->get('key_mail_jet'),true,['version' => 'v3.1']);
$messages = [];
foreach($tab as $value){
$messages[] = [
'From' => [
'Email' => $this->params->get('sender_address'),
'Name' => "SYSTEO - timeSheet"
],
'To' => [
[
'Email' => $value['email'],
'Name' => $value['prenomNom']
]
],
'Subject' => $subject,
'TextPart' => Strip_tags($this->templating->render($template, array('content' => $value['content']))),
'HTMLPart' => $this->templating->render($template, array('content' => $value['content']))
];
}
$body = [
'Messages' => $messages
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
$response->success() && var_dump($response->getData());
}
/*public function sendHTMLEmailWithTemplate($from, $to, $subject, $bodyParams, $template, $replyTo = null, $files = [])
{
$mail = new Swift_Message();
$mail
->setFrom($from)
->setTo($to)
->setCharset('UTF-8')
->setSubject($subject)
->setBody(
$this->templating->render($template, array('content' => $bodyParams)))
->setContentType('text/html');
/*foreach($files as $f){
$mail->attach(\Swift_Attachment::fromPath($f));
}
if (!is_null($replyTo)) {
$mail->setReplyTo($replyTo);
}
try {
$this->mailer->send($mail);
return true;
} catch (\Exception $e) {
return false;
}
}*/
}