4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  4. use Lc\ShopBundle\Model\Merchant;
  5. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Twig\Environment;
  8. class MailUtils
  9. {
  10. const SUBJECT = 'subject' ;
  11. const SUBJECT_PREFIX = 'subject-prefix' ;
  12. const TO_EMAIL = 'to-email' ;
  13. const TO_NAME = 'to-name' ;
  14. const FROM_EMAIL = 'from-email' ;
  15. const FROM_NAME = 'from-name' ;
  16. const REPLY_TO = 'reply-to' ;
  17. const CONTENT_TEMPLATE = 'content-template' ;
  18. const CONTENT_DATA = 'content-data' ;
  19. const ATTACHMENT_DATA = 'attachment-data' ;
  20. const ATTACHMENT_FILENAME = 'attachment-filename' ;
  21. const ATTACHMENT_CONTENT_TYPE = 'attachment-content-type' ;
  22. protected $transport ;
  23. protected $templating ;
  24. protected $parameterBag ;
  25. protected $merchantUtils ;
  26. public function __construct(MailjetTransport $mailjetTransport, Environment $templating,ParameterBagInterface $parameterBag, MerchantUtilsInterface $merchantUtils)
  27. {
  28. $this->transport = $mailjetTransport ;
  29. $this->templating = $templating ;
  30. $this->parameterBag = $parameterBag ;
  31. $this->merchantUtils = $merchantUtils ;
  32. }
  33. public function send($params = [])
  34. {
  35. $merchantCurrent = $this->merchantUtils->getMerchantCurrent() ;
  36. $merchantConfigEmailFrom = $merchantCurrent->getMerchantConfig('email-from') ;
  37. $emailFrom = isset($params[self::FROM_EMAIL]) ? $params[self::FROM_EMAIL] : $merchantConfigEmailFrom ;
  38. $merchantConfigEmailFromName = $merchantCurrent->getMerchantConfig('email-from-name') ;
  39. $emailFromName = isset($params[self::FROM_NAME]) ? $params[self::FROM_NAME] : $merchantConfigEmailFromName ;
  40. $merchantConfigEmailSubjectPrefix = $merchantCurrent->getMerchantConfig('email-subject-prefix') ;
  41. $emailSubjectPrefix = isset($params[self::SUBJECT_PREFIX]) ? $params[self::SUBJECT_PREFIX] : $merchantConfigEmailSubjectPrefix ;
  42. if($emailSubjectPrefix && strlen($emailSubjectPrefix)) {
  43. $emailSubjectPrefix .= ' ' ;
  44. }
  45. $message = new \Swift_Message($emailSubjectPrefix.$params[self::SUBJECT]);
  46. $message->addTo(
  47. $params[self::TO_EMAIL],
  48. isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null)
  49. ->addFrom($emailFrom, $emailFromName)
  50. ->setBody($this->templating->render($params[self::CONTENT_TEMPLATE].'-html.html.twig', $params[self::CONTENT_DATA]), 'text/html')
  51. ->addPart($this->templating->render($params[self::CONTENT_TEMPLATE].'-text.html.twig', $params[self::CONTENT_DATA]));
  52. if(isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
  53. $message->addReplyTo($params[self::REPLY_TO]) ;
  54. }
  55. if(isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
  56. $message->attach(\Swift_Attachment::newInstance(
  57. $params[self::ATTACHMENT_DATA],
  58. $params[self::ATTACHMENT_FILENAME],
  59. $params[self::ATTACHMENT_CONTENT_TYPE]
  60. ));
  61. }
  62. $this->transport->send($message) ;
  63. }
  64. }