/app/bundles/EmailBundle/Swiftmailer/Transport/AbstractTokenArrayTransport.php

https://gitlab.com/mautic-master/mautic · PHP · 288 lines · 172 code · 38 blank · 78 comment · 22 complexity · 65989430cebdf44857da428c618322a8 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Mautic
  4. * @copyright 2015 Mautic Contributors. All rights reserved.
  5. * @author Mautic
  6. * @link http://mautic.org
  7. * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
  8. */
  9. namespace Mautic\EmailBundle\Swiftmailer\Transport;
  10. use Mautic\CoreBundle\Factory\MauticFactory;
  11. use Mautic\EmailBundle\Helper\MailHelper;
  12. use Mautic\EmailBundle\Swiftmailer\Message\MauticMessage;
  13. /**
  14. * Class AbstractTokenArrayTransport
  15. */
  16. abstract class AbstractTokenArrayTransport implements InterfaceTokenTransport
  17. {
  18. /**
  19. * @var \Swift_Message
  20. */
  21. protected $message;
  22. /**
  23. * @var
  24. */
  25. private $dispatcher;
  26. /**
  27. * @var bool
  28. */
  29. protected $started = false;
  30. /**
  31. * @var MauticFactory
  32. */
  33. protected $factory;
  34. /**
  35. * Test if this Transport mechanism has started.
  36. *
  37. * @return bool
  38. */
  39. public function isStarted()
  40. {
  41. return $this->started;
  42. }
  43. /**
  44. * Stop this Transport mechanism.
  45. */
  46. public function stop()
  47. {
  48. }
  49. /**
  50. * Start this Transport mechanism.
  51. */
  52. public function start()
  53. {
  54. }
  55. /**
  56. * Register a plugin in the Transport.
  57. *
  58. * @param \Swift_Events_EventListener $plugin
  59. */
  60. public function registerPlugin(\Swift_Events_EventListener $plugin)
  61. {
  62. $this->getDispatcher()->bindEventListener($plugin);
  63. }
  64. /**
  65. * @return \Swift_Events_SimpleEventDispatcher
  66. */
  67. protected function getDispatcher()
  68. {
  69. if ($this->dispatcher == null) {
  70. $this->dispatcher = new \Swift_Events_SimpleEventDispatcher();
  71. }
  72. return $this->dispatcher;
  73. }
  74. /**
  75. * @param \Swift_Mime_Message $message
  76. * @param null $failedRecipients
  77. *
  78. * @return int
  79. * @throws \Exception
  80. */
  81. abstract public function send(\Swift_Mime_Message $message, &$failedRecipients = null);
  82. /**
  83. * Get the metadata from a MauticMessage
  84. *
  85. * @return array
  86. */
  87. public function getMetadata()
  88. {
  89. return ($this->message instanceof MauticMessage) ? $this->message->getMetadata() : array();
  90. }
  91. /**
  92. * Get attachments from a MauticMessage
  93. *
  94. * @return array
  95. */
  96. public function getAttachments()
  97. {
  98. return ($this->message instanceof MauticMessage) ? $this->message->getAttachments() : array();
  99. }
  100. /**
  101. * Converts \Swift_Message into associative array
  102. *
  103. * @param array $search If the mailer requires tokens in another format than Mautic's, pass array of Mautic tokens to replace
  104. * @param array $replace If the mailer requires tokens in another format than Mautic's, pass array of replacement tokens
  105. * @param bool|false $binaryAttachments True to convert file attachments to binary
  106. * @return array|\Swift_Message
  107. */
  108. protected function messageToArray($search = array(), $replace = array(), $binaryAttachments = false)
  109. {
  110. if (!empty($search)) {
  111. MailHelper::searchReplaceTokens($search, $replace, $this->message);
  112. }
  113. $from = $this->message->getFrom();
  114. $fromEmail = current(array_keys($from));
  115. $fromName = $from[$fromEmail];
  116. $message = array(
  117. 'html' => $this->message->getBody(),
  118. 'text' => MailHelper::getPlainTextFromMessage($this->message),
  119. 'subject' => $this->message->getSubject(),
  120. 'from' => array(
  121. 'name' => $fromName,
  122. 'email' => $fromEmail
  123. )
  124. );
  125. // Generate the recipients
  126. $message['recipients'] = array(
  127. 'to' => array(),
  128. 'cc' => array(),
  129. 'bcc' => array()
  130. );
  131. $to = $this->message->getTo();
  132. foreach ($to as $email => $name) {
  133. $message['recipients']['to'][$email] = array(
  134. 'email' => $email,
  135. 'name' => $name
  136. );
  137. }
  138. $cc = $this->message->getCc();
  139. if (!empty($cc)) {
  140. foreach ($cc as $email => $name) {
  141. $message['recipients']['cc'][$email] = array(
  142. 'email' => $email,
  143. 'name' => $name
  144. );
  145. }
  146. }
  147. $bcc = $this->message->getBcc();
  148. if (!empty($bcc)) {
  149. foreach ($bcc as $email => $name) {
  150. $message['recipients']['bcc'][$email] = array(
  151. 'email' => $email,
  152. 'name' => $name
  153. );
  154. }
  155. }
  156. $replyTo = $this->message->getReplyTo();
  157. if (!empty($replyTo)) {
  158. foreach ($replyTo as $email => $name) {
  159. $message['replyTo'] = array(
  160. 'email' => $email,
  161. 'name' => $name
  162. );
  163. }
  164. }
  165. $returnPath = $this->message->getReturnPath();
  166. if (!empty($returnPath)) {
  167. $message['returnPath'] = $returnPath;
  168. }
  169. // Attachments
  170. $children = $this->message->getChildren();
  171. $attachments = array();
  172. foreach ($children as $child) {
  173. if ($child instanceof \Swift_Attachment) {
  174. $attachments[] = array(
  175. 'type' => $child->getContentType(),
  176. 'name' => $child->getFilename(),
  177. 'content' => $child->getEncoder()->encodeString($child->getBody())
  178. );
  179. }
  180. }
  181. if ($binaryAttachments) {
  182. // Convert attachments to binary if applicable
  183. $message['attachments'] = $attachments;
  184. $fileAttachments = $this->getAttachments();
  185. if (!empty($fileAttachments)) {
  186. foreach ($fileAttachments as $attachment) {
  187. if (file_exists($attachment['filePath']) && is_readable($attachment['filePath'])) {
  188. try {
  189. $swiftAttachment = \Swift_Attachment::fromPath($attachment['filePath']);
  190. if (!empty($attachment['fileName'])) {
  191. $swiftAttachment->setFilename($attachment['fileName']);
  192. }
  193. if (!empty($attachment['contentType'])) {
  194. $swiftAttachment->setContentType($attachment['contentType']);
  195. }
  196. if (!empty($attachment['inline'])) {
  197. $swiftAttachment->setDisposition('inline');
  198. }
  199. $message['attachments'][] = array(
  200. 'type' => $swiftAttachment->getContentType(),
  201. 'name' => $swiftAttachment->getFilename(),
  202. 'content' => $swiftAttachment->getEncoder()->encodeString($swiftAttachment->getBody())
  203. );
  204. } catch (\Exception $e) {
  205. error_log($e);
  206. }
  207. }
  208. }
  209. }
  210. } else {
  211. $message['binary_attachments'] = $attachments;
  212. $message['file_attachments'] = $this->getAttachments();
  213. }
  214. $message['headers'] = array();
  215. $headers = $this->message->getHeaders()->getAll();
  216. /** @var \Swift_Mime_Header $header */
  217. foreach ($headers as $header) {
  218. if ($header->getFieldType() == \Swift_Mime_Header::TYPE_TEXT) {
  219. $message['headers'][$header->getFieldName()] = $header->getFieldBodyModel();
  220. }
  221. }
  222. return $message;
  223. }
  224. /**
  225. * @param MauticFactory $factory
  226. */
  227. public function setMauticFactory(MauticFactory $factory)
  228. {
  229. $this->factory = $factory;
  230. }
  231. /**
  232. * @param \Exception|string $exception
  233. *
  234. * @throws \Exception
  235. */
  236. protected function throwException($exception)
  237. {
  238. if (!$exception instanceof \Exception) {
  239. $exception = new \Swift_TransportException($exception);
  240. }
  241. if ($evt = $this->getDispatcher()->createTransportExceptionEvent($this, $exception)) {
  242. $this->getDispatcher()->dispatchEvent($evt, 'exceptionThrown');
  243. if (!$evt->bubbleCancelled()) {
  244. throw $exception;
  245. }
  246. } else {
  247. throw $exception;
  248. }
  249. }
  250. }