Transactional.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace Dotdigitalgroup\Email\Helper;
  3. use Zend\Mail\Transport\SmtpOptions;
  4. /**
  5. * Transactional emails configuration data values.
  6. */
  7. class Transactional extends \Magento\Framework\App\Helper\AbstractHelper
  8. {
  9. const XML_PATH_DDG_TRANSACTIONAL_ENABLED = 'transactional_emails/ddg_transactional/enabled';
  10. const XML_PATH_DDG_TRANSACTIONAL_HOST = 'transactional_emails/ddg_transactional/host';
  11. const XML_PATH_DDG_TRANSACTIONAL_USERNAME = 'transactional_emails/ddg_transactional/username';
  12. const XML_PATH_DDG_TRANSACTIONAL_PASSWORD = 'transactional_emails/ddg_transactional/password';
  13. const XML_PATH_DDG_TRANSACTIONAL_PORT = 'transactional_emails/ddg_transactional/port';
  14. const XML_PATH_DDG_TRANSACTIONAL_DEBUG = 'transactional_emails/ddg_transactional/debug';
  15. /**
  16. * @var \Magento\Framework\Encryption\EncryptorInterface
  17. */
  18. private $encryptor;
  19. /**
  20. * Transactional constructor.
  21. *
  22. * @param \Magento\Framework\App\Helper\Context $context
  23. * @var \Magento\Framework\Encryption\EncryptorInterface $encryptor
  24. */
  25. public function __construct(
  26. \Magento\Framework\App\Helper\Context $context,
  27. \Magento\Framework\Encryption\EncryptorInterface $encryptor
  28. ) {
  29. $this->encryptor = $encryptor;
  30. parent::__construct($context);
  31. }
  32. /**
  33. * Is transactional email enabled.
  34. *
  35. * @param int $storeId
  36. *
  37. * @return bool
  38. */
  39. public function isEnabled($storeId)
  40. {
  41. return $this->scopeConfig->isSetFlag(
  42. self::XML_PATH_DDG_TRANSACTIONAL_ENABLED,
  43. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  44. $storeId
  45. );
  46. }
  47. /**
  48. * Get transactional email host.
  49. *
  50. * @param int $storeId
  51. *
  52. * @return boolean|string
  53. */
  54. public function getSmtpHost($storeId)
  55. {
  56. return $this->scopeConfig->getValue(
  57. self::XML_PATH_DDG_TRANSACTIONAL_HOST,
  58. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  59. $storeId
  60. );
  61. }
  62. /**
  63. * Get smtp username.
  64. *
  65. * @param int $storeId
  66. *
  67. * @return boolean|string
  68. */
  69. private function getSmtpUsername($storeId = null)
  70. {
  71. return $this->scopeConfig->getValue(
  72. self::XML_PATH_DDG_TRANSACTIONAL_USERNAME,
  73. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  74. $storeId
  75. );
  76. }
  77. /**
  78. * Get smtp password.
  79. *
  80. * @param int $storeId
  81. *
  82. * @return boolean|string
  83. */
  84. private function getSmtpPassword($storeId = null)
  85. {
  86. $value = $this->scopeConfig->getValue(
  87. self::XML_PATH_DDG_TRANSACTIONAL_PASSWORD,
  88. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  89. $storeId
  90. );
  91. return $this->encryptor->decrypt($value);
  92. }
  93. /**
  94. * Get smtp port.
  95. *
  96. * @param int $storeId
  97. *
  98. * @return boolean|string
  99. */
  100. private function getSmtpPort($storeId)
  101. {
  102. return $this->scopeConfig->getValue(
  103. self::XML_PATH_DDG_TRANSACTIONAL_PORT,
  104. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  105. $storeId
  106. );
  107. }
  108. /**
  109. * Get transactional log enabled.
  110. *
  111. * @param int $storeId
  112. *
  113. * @return bool
  114. */
  115. private function isDebugEnabled($storeId)
  116. {
  117. return $this->scopeConfig->isSetFlag(
  118. self::XML_PATH_DDG_TRANSACTIONAL_DEBUG,
  119. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  120. $storeId
  121. );
  122. }
  123. /**
  124. * Get config values for transport.
  125. *
  126. * @param int $storeId
  127. *
  128. * @return array
  129. */
  130. public function getTransportConfig($storeId)
  131. {
  132. $config = [
  133. 'port' => $this->getSmtpPort($storeId),
  134. 'auth' => 'login',
  135. 'username' => $this->getSmtpUsername($storeId),
  136. 'password' => $this->getSmtpPassword($storeId),
  137. 'ssl' => 'tls',
  138. ];
  139. if ($this->isDebugEnabled($storeId)) {
  140. $this->_logger->debug('Mail transport config : ' . implode(',', $config));
  141. }
  142. return $config;
  143. }
  144. /**
  145. * @param int $storeId
  146. *
  147. * @return SmtpOptions
  148. */
  149. public function getSmtpOptions($storeId)
  150. {
  151. return new SmtpOptions(
  152. [
  153. 'host' => $this->getSmtpHost($storeId),
  154. 'port' => $this->getSmtpPort($storeId),
  155. 'connection_class' => 'login',
  156. 'connection_config' =>
  157. [
  158. 'username' => $this->getSmtpUsername($storeId),
  159. 'password' => $this->getSmtpPassword($storeId),
  160. 'ssl' => 'tls'
  161. ]
  162. ]
  163. );
  164. }
  165. /**
  166. * Check if the template code is containing dotmailer.
  167. *
  168. * @param string $templateCode
  169. * @return bool
  170. */
  171. public function isDotmailerTemplate($templateCode)
  172. {
  173. preg_match("/\_\d{1,10}$/", $templateCode, $matches);
  174. if (count($matches)) {
  175. return true;
  176. }
  177. return false;
  178. }
  179. }