You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MailUtils.php 3.4KB

4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 CONTENT_TEMPLATE = 'content-template' ;
  17. const CONTENT_DATA = 'content-data' ;
  18. const ATTACHMENT_DATA = 'attachment-data' ;
  19. const ATTACHMENT_FILENAME = 'attachment-filename' ;
  20. const ATTACHMENT_CONTENT_TYPE = 'attachment-content-type' ;
  21. protected $transport ;
  22. protected $templating ;
  23. protected $parameterBag ;
  24. protected $merchantUtils ;
  25. public function __construct(MailjetTransport $mailjetTransport, Environment $templating,ParameterBagInterface $parameterBag, MerchantUtilsInterface $merchantUtils)
  26. {
  27. $this->transport = $mailjetTransport ;
  28. $this->templating = $templating ;
  29. $this->parameterBag = $parameterBag ;
  30. $this->merchantUtils = $merchantUtils ;
  31. }
  32. public function send($params = [])
  33. {
  34. $merchantCurrent = $this->merchantUtils->getMerchantCurrent() ;
  35. $merchantConfigEmailFrom = $merchantCurrent->getMerchantConfig('email-from') ;
  36. $emailFrom = isset($params[self::FROM_EMAIL]) ? $params[self::FROM_EMAIL] : $merchantConfigEmailFrom ;
  37. $merchantConfigEmailFromName = $merchantCurrent->getMerchantConfig('email-from-name') ;
  38. $emailFromName = isset($params[self::FROM_NAME]) ? $params[self::FROM_NAME] : $merchantConfigEmailFromName ;
  39. $merchantConfigEmailSubjectPrefix = $merchantCurrent->getMerchantConfig('email-subject-prefix') ;
  40. $emailSubjectPrefix = isset($params[self::SUBJECT_PREFIX]) ? $params[self::SUBJECT_PREFIX] : $merchantConfigEmailSubjectPrefix ;
  41. if($emailSubjectPrefix && strlen($emailSubjectPrefix)) {
  42. $emailSubjectPrefix .= ' ' ;
  43. }
  44. $message = new \Swift_Message($emailSubjectPrefix.$params[self::SUBJECT]);
  45. $message->addTo(
  46. $params[self::TO_EMAIL],
  47. isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null)
  48. ->addFrom($emailFrom, $emailFromName)
  49. ->setBody($this->templating->render($params[self::CONTENT_TEMPLATE].'-html.html.twig', $params[self::CONTENT_DATA]), 'text/html')
  50. ->addPart($this->templating->render($params[self::CONTENT_TEMPLATE].'-text.html.twig', $params[self::CONTENT_DATA]));
  51. if(isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
  52. $message->attach(\Swift_Attachment::newInstance(
  53. $params[self::ATTACHMENT_DATA],
  54. $params[self::ATTACHMENT_FILENAME],
  55. $params[self::ATTACHMENT_CONTENT_TYPE]
  56. ));
  57. }
  58. $this->transport->send($message) ;
  59. }
  60. }