Browse Source

MailUtils

feature/export_comptable
Guillaume 4 years ago
parent
commit
00e6f3201b
2 changed files with 53 additions and 1 deletions
  1. +45
    -0
      ShopBundle/Services/MailUtils.php
  2. +8
    -1
      ShopBundle/Services/UtilsManager.php

+ 45
- 0
ShopBundle/Services/MailUtils.php View File

@@ -0,0 +1,45 @@
<?php

namespace Lc\ShopBundle\Services ;

use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Twig\Environment;

class MailUtils
{
const SUBJECT = 'subject' ;
const TO_EMAIL = 'to-email' ;
const TO_NAME = 'to-name' ;
const FROM_EMAIL = 'from-email' ;
const FROM_NAME = 'from-name' ;
const CONTENT_TEMPLATE = 'content-template' ;
const CONTENT_DATA = 'content-data' ;

protected $transport ;
protected $templating ;
protected $parameterBag ;

public function __construct(MailjetTransport $mailjetTransport, Environment $templating,ParameterBagInterface $parameterBag)
{
$this->transport = $mailjetTransport ;
$this->templating = $templating ;
$this->parameterBag = $parameterBag ;
}

public function send($params = [])
{
$message = new \Swift_Message($params[self::SUBJECT]);
$message->addTo(
$params[self::TO_EMAIL],
isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null)
->addFrom(
isset($params[self::FROM_EMAIL]) ? $params[self::FROM_EMAIL] : $this->parameterBag->get('app.noreply_email'),
isset($params[self::FROM_NAME]) ? $params[self::FROM_NAME] : $this->parameterBag->get('app.site_name'))
->setBody($this->templating->render($params[self::CONTENT_TEMPLATE].'-html.html.twig', $params[self::CONTENT_DATA]), 'text/html')
->addPart($this->templating->render($params[self::CONTENT_TEMPLATE].'-text.html.twig', $params[self::CONTENT_DATA]))
;

$this->transport->send($message) ;
}
}

+ 8
- 1
ShopBundle/Services/UtilsManager.php View File

@@ -19,6 +19,7 @@ class UtilsManager
protected $deliveryUtils ;
protected $creditUtils ;
protected $documentUtils ;
protected $mailUtils ;

public function __construct(
Utils $utils,
@@ -29,7 +30,8 @@ class UtilsManager
PriceUtilsInterface $priceUtils,
DeliveryUtilsInterface $deliveryUtils,
CreditUtils $creditUtils,
DocumentUtils $documentUtils
DocumentUtils $documentUtils,
MailUtils $mailUtils
)
{
$this->utils = $utils ;
@@ -41,6 +43,7 @@ class UtilsManager
$this->deliveryUtils = $deliveryUtils ;
$this->creditUtils = $creditUtils ;
$this->documentUtils = $documentUtils ;
$this->mailUtils = $mailUtils ;
}

public function getUtils(): Utils
@@ -88,4 +91,8 @@ class UtilsManager
return $this->documentUtils ;
}

public function getMailUtils(): MailUtils
{
return $this->mailUtils ;
}
}

Loading…
Cancel
Save