Browse Source

Redirection email

feature/export_comptable
Fab 4 years ago
parent
commit
ef5cc12f3c
1 changed files with 51 additions and 43 deletions
  1. +51
    -43
      ShopBundle/Services/MailUtils.php

+ 51
- 43
ShopBundle/Services/MailUtils.php View File

<?php <?php


namespace Lc\ShopBundle\Services ;
namespace Lc\ShopBundle\Services;


use Lc\ShopBundle\Context\MerchantUtilsInterface; use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Model\Merchant; use Lc\ShopBundle\Model\Merchant;


class MailUtils class MailUtils
{ {
const SUBJECT = 'subject' ;
const SUBJECT_PREFIX = 'subject-prefix' ;
const TO_EMAIL = 'to-email' ;
const TO_NAME = 'to-name' ;
const FROM_EMAIL = 'from-email' ;
const FROM_NAME = 'from-name' ;
const REPLY_TO = 'reply-to' ;
const CONTENT_TEMPLATE = 'content-template' ;
const CONTENT_DATA = 'content-data' ;
const ATTACHMENT_DATA = 'attachment-data' ;
const ATTACHMENT_FILENAME = 'attachment-filename' ;
const ATTACHMENT_CONTENT_TYPE = 'attachment-content-type' ;
protected $transport ;
protected $templating ;
protected $parameterBag ;
protected $merchantUtils ;
public function __construct(MailjetTransport $mailjetTransport, Environment $templating,ParameterBagInterface $parameterBag, MerchantUtilsInterface $merchantUtils)
const SUBJECT = 'subject';
const SUBJECT_PREFIX = 'subject-prefix';
const TO_EMAIL = 'to-email';
const TO_NAME = 'to-name';
const FROM_EMAIL = 'from-email';
const FROM_NAME = 'from-name';
const REPLY_TO = 'reply-to';
const CONTENT_TEMPLATE = 'content-template';
const CONTENT_DATA = 'content-data';
const ATTACHMENT_DATA = 'attachment-data';
const ATTACHMENT_FILENAME = 'attachment-filename';
const ATTACHMENT_CONTENT_TYPE = 'attachment-content-type';
protected $transport;
protected $templating;
protected $parameterBag;
protected $merchantUtils;
public function __construct(MailjetTransport $mailjetTransport, Environment $templating, ParameterBagInterface $parameterBag, MerchantUtilsInterface $merchantUtils)
{ {
$this->transport = $mailjetTransport ;
$this->templating = $templating ;
$this->parameterBag = $parameterBag ;
$this->merchantUtils = $merchantUtils ;
$this->transport = $mailjetTransport;
$this->templating = $templating;
$this->parameterBag = $parameterBag;
$this->merchantUtils = $merchantUtils;
} }


public function send($params = []) public function send($params = [])
{ {
$merchantCurrent = $this->merchantUtils->getMerchantCurrent() ;
$merchantCurrent = $this->merchantUtils->getMerchantCurrent();


$merchantConfigEmailFrom = $merchantCurrent->getMerchantConfig('email-from') ;
$emailFrom = isset($params[self::FROM_EMAIL]) ? $params[self::FROM_EMAIL] : $merchantConfigEmailFrom ;
$merchantConfigEmailFrom = $merchantCurrent->getMerchantConfig('email-from');
$emailFrom = isset($params[self::FROM_EMAIL]) ? $params[self::FROM_EMAIL] : $merchantConfigEmailFrom;


$merchantConfigEmailFromName = $merchantCurrent->getMerchantConfig('email-from-name') ;
$emailFromName = isset($params[self::FROM_NAME]) ? $params[self::FROM_NAME] : $merchantConfigEmailFromName ;
$merchantConfigEmailFromName = $merchantCurrent->getMerchantConfig('email-from-name');
$emailFromName = isset($params[self::FROM_NAME]) ? $params[self::FROM_NAME] : $merchantConfigEmailFromName;


$merchantConfigEmailSubjectPrefix = $merchantCurrent->getMerchantConfig('email-subject-prefix') ;
$emailSubjectPrefix = isset($params[self::SUBJECT_PREFIX]) ? $params[self::SUBJECT_PREFIX] : $merchantConfigEmailSubjectPrefix ;
if($emailSubjectPrefix && strlen($emailSubjectPrefix)) {
$emailSubjectPrefix .= ' ' ;
$merchantConfigEmailSubjectPrefix = $merchantCurrent->getMerchantConfig('email-subject-prefix');
$emailSubjectPrefix = isset($params[self::SUBJECT_PREFIX]) ? $params[self::SUBJECT_PREFIX] : $merchantConfigEmailSubjectPrefix;
if ($emailSubjectPrefix && strlen($emailSubjectPrefix)) {
$emailSubjectPrefix .= ' ';
} }


$message = new \Swift_Message($emailSubjectPrefix.$params[self::SUBJECT]);
$message->addTo(
$message = new \Swift_Message($emailSubjectPrefix . $params[self::SUBJECT]);


if ($this->parameterBag->get('mailjet.dev.redirect.active')==1) {
$message->addTo($this->parameterBag->get('mailjet.dev.redirect.email'),
isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null);
} else {
$message->addTo(
$params[self::TO_EMAIL], $params[self::TO_EMAIL],
isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null)
->addFrom($emailFrom, $emailFromName)
->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]));
isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null);
}

$message->addFrom($emailFrom, $emailFromName)
->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]));


if(isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
$message->addReplyTo($params[self::REPLY_TO]) ;
if (isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
$message->addReplyTo($params[self::REPLY_TO]);
} }


if(isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
if (isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
$message->attach(\Swift_Attachment::newInstance( $message->attach(\Swift_Attachment::newInstance(
$params[self::ATTACHMENT_DATA], $params[self::ATTACHMENT_DATA],
$params[self::ATTACHMENT_FILENAME], $params[self::ATTACHMENT_FILENAME],
)); ));
} }


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

Loading…
Cancel
Save