Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

46 rindas
1.9KB

  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use Twig\Environment;
  6. class MailUtils
  7. {
  8. const SUBJECT = 'subject' ;
  9. const TO_EMAIL = 'to-email' ;
  10. const TO_NAME = 'to-name' ;
  11. const FROM_EMAIL = 'from-email' ;
  12. const FROM_NAME = 'from-name' ;
  13. const CONTENT_TEMPLATE = 'content-template' ;
  14. const CONTENT_DATA = 'content-data' ;
  15. protected $transport ;
  16. protected $templating ;
  17. protected $parameterBag ;
  18. public function __construct(MailjetTransport $mailjetTransport, Environment $templating,ParameterBagInterface $parameterBag)
  19. {
  20. $this->transport = $mailjetTransport ;
  21. $this->templating = $templating ;
  22. $this->parameterBag = $parameterBag ;
  23. }
  24. public function send($params = [])
  25. {
  26. $message = new \Swift_Message($params[self::SUBJECT]);
  27. $message->addTo(
  28. $params[self::TO_EMAIL],
  29. isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null)
  30. ->addFrom(
  31. isset($params[self::FROM_EMAIL]) ? $params[self::FROM_EMAIL] : $this->parameterBag->get('app.noreply_email'),
  32. isset($params[self::FROM_NAME]) ? $params[self::FROM_NAME] : $this->parameterBag->get('app.site_name'))
  33. ->setBody($this->templating->render($params[self::CONTENT_TEMPLATE].'-html.html.twig', $params[self::CONTENT_DATA]), 'text/html')
  34. ->addPart($this->templating->render($params[self::CONTENT_TEMPLATE].'-text.html.twig', $params[self::CONTENT_DATA]))
  35. ;
  36. $this->transport->send($message) ;
  37. }
  38. }