MessageFactory.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\MonologBundle\SwiftMailer;
  11. /**
  12. * Helps create Swift_Message objects, lazily
  13. *
  14. * @author Ryan Weaver <ryan@knpuniversity.com>
  15. */
  16. class MessageFactory
  17. {
  18. private $mailer;
  19. private $fromEmail;
  20. private $toEmail;
  21. private $subject;
  22. private $contentType;
  23. public function __construct(\Swift_Mailer $mailer, $fromEmail, $toEmail, $subject, $contentType = null)
  24. {
  25. $this->mailer = $mailer;
  26. $this->fromEmail = $fromEmail;
  27. $this->toEmail = $toEmail;
  28. $this->subject = $subject;
  29. $this->contentType = $contentType;
  30. }
  31. /**
  32. * Creates a Swift_Message template that will be used to send the log message
  33. *
  34. * @param string $content formatted email body to be sent
  35. * @param array $records Log records that formed the content
  36. * @return \Swift_Message
  37. */
  38. public function createMessage($content, array $records)
  39. {
  40. /** @var \Swift_Message $message */
  41. $message = $this->mailer->createMessage();
  42. $message->setTo($this->toEmail);
  43. $message->setFrom($this->fromEmail);
  44. $message->setSubject($this->subject);
  45. if ($this->contentType) {
  46. $message->setContentType($this->contentType);
  47. }
  48. return $message;
  49. }
  50. }