EmailNotification.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Mail\Template\SenderResolverInterface;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. use Magento\Framework\Mail\Template\TransportBuilder;
  12. use Magento\Customer\Helper\View as CustomerViewHelper;
  13. use Magento\Customer\Api\Data\CustomerInterface;
  14. use Magento\Framework\Reflection\DataObjectProcessor;
  15. use Magento\Framework\Exception\LocalizedException;
  16. /**
  17. * Customer email notification
  18. *
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class EmailNotification implements EmailNotificationInterface
  22. {
  23. /**#@+
  24. * Configuration paths for email templates and identities
  25. */
  26. const XML_PATH_FORGOT_EMAIL_IDENTITY = 'customer/password/forgot_email_identity';
  27. const XML_PATH_RESET_PASSWORD_TEMPLATE = 'customer/password/reset_password_template';
  28. const XML_PATH_CHANGE_EMAIL_TEMPLATE = 'customer/account_information/change_email_template';
  29. const XML_PATH_CHANGE_EMAIL_AND_PASSWORD_TEMPLATE =
  30. 'customer/account_information/change_email_and_password_template';
  31. const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'customer/password/forgot_email_template';
  32. const XML_PATH_REMIND_EMAIL_TEMPLATE = 'customer/password/remind_email_template';
  33. const XML_PATH_REGISTER_EMAIL_IDENTITY = 'customer/create_account/email_identity';
  34. const XML_PATH_REGISTER_EMAIL_TEMPLATE = 'customer/create_account/email_template';
  35. const XML_PATH_REGISTER_NO_PASSWORD_EMAIL_TEMPLATE = 'customer/create_account/email_no_password_template';
  36. const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'customer/create_account/email_confirmation_template';
  37. const XML_PATH_CONFIRMED_EMAIL_TEMPLATE = 'customer/create_account/email_confirmed_template';
  38. /**
  39. * self::NEW_ACCOUNT_EMAIL_REGISTERED welcome email, when confirmation is disabled
  40. * and password is set
  41. * self::NEW_ACCOUNT_EMAIL_REGISTERED_NO_PASSWORD welcome email, when confirmation is disabled
  42. * and password is not set
  43. * self::NEW_ACCOUNT_EMAIL_CONFIRMED welcome email, when confirmation is enabled
  44. * and password is set
  45. * self::NEW_ACCOUNT_EMAIL_CONFIRMATION email with confirmation link
  46. */
  47. const TEMPLATE_TYPES = [
  48. self::NEW_ACCOUNT_EMAIL_REGISTERED => self::XML_PATH_REGISTER_EMAIL_TEMPLATE,
  49. self::NEW_ACCOUNT_EMAIL_REGISTERED_NO_PASSWORD => self::XML_PATH_REGISTER_NO_PASSWORD_EMAIL_TEMPLATE,
  50. self::NEW_ACCOUNT_EMAIL_CONFIRMED => self::XML_PATH_CONFIRMED_EMAIL_TEMPLATE,
  51. self::NEW_ACCOUNT_EMAIL_CONFIRMATION => self::XML_PATH_CONFIRM_EMAIL_TEMPLATE,
  52. ];
  53. /**#@-*/
  54. /**#@-*/
  55. private $customerRegistry;
  56. /**
  57. * @var StoreManagerInterface
  58. */
  59. private $storeManager;
  60. /**
  61. * @var TransportBuilder
  62. */
  63. private $transportBuilder;
  64. /**
  65. * @var CustomerViewHelper
  66. */
  67. protected $customerViewHelper;
  68. /**
  69. * @var DataObjectProcessor
  70. */
  71. protected $dataProcessor;
  72. /**
  73. * @var ScopeConfigInterface
  74. */
  75. private $scopeConfig;
  76. /**
  77. * @var SenderResolverInterface
  78. */
  79. private $senderResolver;
  80. /**
  81. * @param CustomerRegistry $customerRegistry
  82. * @param StoreManagerInterface $storeManager
  83. * @param TransportBuilder $transportBuilder
  84. * @param CustomerViewHelper $customerViewHelper
  85. * @param DataObjectProcessor $dataProcessor
  86. * @param ScopeConfigInterface $scopeConfig
  87. * @param SenderResolverInterface|null $senderResolver
  88. */
  89. public function __construct(
  90. CustomerRegistry $customerRegistry,
  91. StoreManagerInterface $storeManager,
  92. TransportBuilder $transportBuilder,
  93. CustomerViewHelper $customerViewHelper,
  94. DataObjectProcessor $dataProcessor,
  95. ScopeConfigInterface $scopeConfig,
  96. SenderResolverInterface $senderResolver = null
  97. ) {
  98. $this->customerRegistry = $customerRegistry;
  99. $this->storeManager = $storeManager;
  100. $this->transportBuilder = $transportBuilder;
  101. $this->customerViewHelper = $customerViewHelper;
  102. $this->dataProcessor = $dataProcessor;
  103. $this->scopeConfig = $scopeConfig;
  104. $this->senderResolver = $senderResolver ?: ObjectManager::getInstance()->get(SenderResolverInterface::class);
  105. }
  106. /**
  107. * Send notification to customer when email or/and password changed
  108. *
  109. * @param CustomerInterface $savedCustomer
  110. * @param string $origCustomerEmail
  111. * @param bool $isPasswordChanged
  112. * @return void
  113. */
  114. public function credentialsChanged(
  115. CustomerInterface $savedCustomer,
  116. $origCustomerEmail,
  117. $isPasswordChanged = false
  118. ) {
  119. if ($origCustomerEmail != $savedCustomer->getEmail()) {
  120. if ($isPasswordChanged) {
  121. $this->emailAndPasswordChanged($savedCustomer, $origCustomerEmail);
  122. $this->emailAndPasswordChanged($savedCustomer, $savedCustomer->getEmail());
  123. return;
  124. }
  125. $this->emailChanged($savedCustomer, $origCustomerEmail);
  126. $this->emailChanged($savedCustomer, $savedCustomer->getEmail());
  127. return;
  128. }
  129. if ($isPasswordChanged) {
  130. $this->passwordReset($savedCustomer);
  131. }
  132. }
  133. /**
  134. * Send email to customer when his email and password is changed
  135. *
  136. * @param CustomerInterface $customer
  137. * @param string $email
  138. * @return void
  139. */
  140. private function emailAndPasswordChanged(CustomerInterface $customer, $email)
  141. {
  142. $storeId = $customer->getStoreId();
  143. if (!$storeId) {
  144. $storeId = $this->getWebsiteStoreId($customer);
  145. }
  146. $customerEmailData = $this->getFullCustomerObject($customer);
  147. $this->sendEmailTemplate(
  148. $customer,
  149. self::XML_PATH_CHANGE_EMAIL_AND_PASSWORD_TEMPLATE,
  150. self::XML_PATH_FORGOT_EMAIL_IDENTITY,
  151. ['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)],
  152. $storeId,
  153. $email
  154. );
  155. }
  156. /**
  157. * Send email to customer when his email is changed
  158. *
  159. * @param CustomerInterface $customer
  160. * @param string $email
  161. * @return void
  162. */
  163. private function emailChanged(CustomerInterface $customer, $email)
  164. {
  165. $storeId = $customer->getStoreId();
  166. if (!$storeId) {
  167. $storeId = $this->getWebsiteStoreId($customer);
  168. }
  169. $customerEmailData = $this->getFullCustomerObject($customer);
  170. $this->sendEmailTemplate(
  171. $customer,
  172. self::XML_PATH_CHANGE_EMAIL_TEMPLATE,
  173. self::XML_PATH_FORGOT_EMAIL_IDENTITY,
  174. ['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)],
  175. $storeId,
  176. $email
  177. );
  178. }
  179. /**
  180. * Send email to customer when his password is reset
  181. *
  182. * @param CustomerInterface $customer
  183. * @return void
  184. */
  185. private function passwordReset(CustomerInterface $customer)
  186. {
  187. $storeId = $customer->getStoreId();
  188. if (!$storeId) {
  189. $storeId = $this->getWebsiteStoreId($customer);
  190. }
  191. $customerEmailData = $this->getFullCustomerObject($customer);
  192. $this->sendEmailTemplate(
  193. $customer,
  194. self::XML_PATH_RESET_PASSWORD_TEMPLATE,
  195. self::XML_PATH_FORGOT_EMAIL_IDENTITY,
  196. ['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)],
  197. $storeId
  198. );
  199. }
  200. /**
  201. * Send corresponding email template
  202. *
  203. * @param CustomerInterface $customer
  204. * @param string $template configuration path of email template
  205. * @param string $sender configuration path of email identity
  206. * @param array $templateParams
  207. * @param int|null $storeId
  208. * @param string $email
  209. * @return void
  210. * @throws \Magento\Framework\Exception\MailException
  211. */
  212. private function sendEmailTemplate(
  213. $customer,
  214. $template,
  215. $sender,
  216. $templateParams = [],
  217. $storeId = null,
  218. $email = null
  219. ) {
  220. $templateId = $this->scopeConfig->getValue($template, 'store', $storeId);
  221. if ($email === null) {
  222. $email = $customer->getEmail();
  223. }
  224. /** @var array $from */
  225. $from = $this->senderResolver->resolve(
  226. $this->scopeConfig->getValue($sender, 'store', $storeId),
  227. $storeId
  228. );
  229. $transport = $this->transportBuilder->setTemplateIdentifier($templateId)
  230. ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
  231. ->setTemplateVars($templateParams)
  232. ->setFrom($from)
  233. ->addTo($email, $this->customerViewHelper->getCustomerName($customer))
  234. ->getTransport();
  235. $transport->sendMessage();
  236. }
  237. /**
  238. * Create an object with data merged from Customer and CustomerSecure
  239. *
  240. * @param CustomerInterface $customer
  241. * @return \Magento\Customer\Model\Data\CustomerSecure
  242. */
  243. private function getFullCustomerObject($customer)
  244. {
  245. // No need to flatten the custom attributes or nested objects since the only usage is for email templates and
  246. // object passed for events
  247. $mergedCustomerData = $this->customerRegistry->retrieveSecureData($customer->getId());
  248. $customerData = $this->dataProcessor
  249. ->buildOutputDataArray($customer, \Magento\Customer\Api\Data\CustomerInterface::class);
  250. $mergedCustomerData->addData($customerData);
  251. $mergedCustomerData->setData('name', $this->customerViewHelper->getCustomerName($customer));
  252. return $mergedCustomerData;
  253. }
  254. /**
  255. * Get either first store ID from a set website or the provided as default
  256. *
  257. * @param CustomerInterface $customer
  258. * @param int|string|null $defaultStoreId
  259. * @return int
  260. */
  261. private function getWebsiteStoreId($customer, $defaultStoreId = null)
  262. {
  263. if ($customer->getWebsiteId() != 0 && empty($defaultStoreId)) {
  264. $storeIds = $this->storeManager->getWebsite($customer->getWebsiteId())->getStoreIds();
  265. $defaultStoreId = reset($storeIds);
  266. }
  267. return $defaultStoreId;
  268. }
  269. /**
  270. * Send email with new customer password
  271. *
  272. * @param CustomerInterface $customer
  273. * @return void
  274. */
  275. public function passwordReminder(CustomerInterface $customer)
  276. {
  277. $storeId = $customer->getStoreId();
  278. if (!$storeId) {
  279. $storeId = $this->getWebsiteStoreId($customer);
  280. }
  281. $customerEmailData = $this->getFullCustomerObject($customer);
  282. $this->sendEmailTemplate(
  283. $customer,
  284. self::XML_PATH_REMIND_EMAIL_TEMPLATE,
  285. self::XML_PATH_FORGOT_EMAIL_IDENTITY,
  286. ['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)],
  287. $storeId
  288. );
  289. }
  290. /**
  291. * Send email with reset password confirmation link
  292. *
  293. * @param CustomerInterface $customer
  294. * @return void
  295. */
  296. public function passwordResetConfirmation(CustomerInterface $customer)
  297. {
  298. $storeId = $this->storeManager->getStore()->getId();
  299. if (!$storeId) {
  300. $storeId = $this->getWebsiteStoreId($customer);
  301. }
  302. $customerEmailData = $this->getFullCustomerObject($customer);
  303. $this->sendEmailTemplate(
  304. $customer,
  305. self::XML_PATH_FORGOT_EMAIL_TEMPLATE,
  306. self::XML_PATH_FORGOT_EMAIL_IDENTITY,
  307. ['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)],
  308. $storeId
  309. );
  310. }
  311. /**
  312. * Send email with new account related information
  313. *
  314. * @param CustomerInterface $customer
  315. * @param string $type
  316. * @param string $backUrl
  317. * @param string $storeId
  318. * @param string $sendemailStoreId
  319. * @return void
  320. * @throws LocalizedException
  321. */
  322. public function newAccount(
  323. CustomerInterface $customer,
  324. $type = self::NEW_ACCOUNT_EMAIL_REGISTERED,
  325. $backUrl = '',
  326. $storeId = 0,
  327. $sendemailStoreId = null
  328. ) {
  329. $types = self::TEMPLATE_TYPES;
  330. if (!isset($types[$type])) {
  331. throw new LocalizedException(
  332. __('The transactional account email type is incorrect. Verify and try again.')
  333. );
  334. }
  335. if (!$storeId) {
  336. $storeId = $this->getWebsiteStoreId($customer, $sendemailStoreId);
  337. }
  338. $store = $this->storeManager->getStore($customer->getStoreId());
  339. $customerEmailData = $this->getFullCustomerObject($customer);
  340. $this->sendEmailTemplate(
  341. $customer,
  342. $types[$type],
  343. self::XML_PATH_REGISTER_EMAIL_IDENTITY,
  344. ['customer' => $customerEmailData, 'back_url' => $backUrl, 'store' => $store],
  345. $storeId
  346. );
  347. }
  348. }