No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

46 líneas
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. }