Customer.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\email;
  10. use Yii;
  11. use fecshop\services\Service;
  12. /**
  13. * customer email services
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class Customer extends Service
  18. {
  19. /**
  20. * 邮件模板部分配置.
  21. */
  22. public $emailTheme;
  23. /**
  24. * 注册账户是否需要邮件激活
  25. */
  26. public $registerAccountIsNeedEnableByEmail = false;
  27. /**
  28. * 注册账户激活邮件的token的过期时间。
  29. */
  30. public $registerAccountEnableTokenExpire = 86400;
  31. /**
  32. * @param $emailInfo | Array ,数组格式格式如下:
  33. * [ 'email' => 'xx@xx.com' , [...] ] 其中email是必须有的数组key,对于其他的,
  34. * 可以根据功能添加,添加后,可以在邮件模板的$params中调用,譬如调用email为 $params['email']
  35. * @return boolean , 如果发送成功,则返回true。
  36. * 该功能为:给客户注册用户发送邮件,使用该函数的格式如下:
  37. * Yii::$service->email->customer->sendRegisterEmail($emailInfo);
  38. */
  39. public function sendRegisterEmail($emailInfo)
  40. {
  41. $toEmail = $emailInfo['email'];
  42. $registerInfo = $this->emailTheme['register'];
  43. if (isset($registerInfo['enable']) && $registerInfo['enable']) {
  44. $mailerConfigParam = '';
  45. if (isset($registerInfo['mailerConfig']) && $registerInfo['mailerConfig']) {
  46. $mailerConfigParam = $registerInfo['mailerConfig'];
  47. }
  48. if (isset($registerInfo['widget']) && $registerInfo['widget']) {
  49. $widget = $registerInfo['widget'];
  50. }
  51. if (isset($registerInfo['viewPath']) && $registerInfo['viewPath']) {
  52. $viewPath = $registerInfo['viewPath'];
  53. }
  54. if ($widget && $viewPath) {
  55. list($subject, $htmlBody) = Yii::$service->email->getSubjectAndBody($widget, $viewPath, '', $emailInfo);
  56. $sendInfo = [
  57. 'to' => $toEmail,
  58. 'subject' => $subject,
  59. 'htmlBody' => $htmlBody,
  60. 'senderName'=> Yii::$service->store->currentStore,
  61. ];
  62. Yii::$service->email->send($sendInfo, $mailerConfigParam);
  63. return true;
  64. }
  65. }
  66. }
  67. /**
  68. * @param $emailInfo | Array ,数组格式格式如下:
  69. * [ 'email' => 'xx@xx.com' , [...] ] 其中email是必须有的数组key,对于其他的,
  70. * 可以根据功能添加,添加后,可以在邮件模板的$params中调用,譬如调用email为 $params['email']
  71. * @return boolean , 如果发送成功,则返回true。
  72. * 客户登录账号发送邮件
  73. */
  74. public function sendLoginEmail($emailInfo)
  75. {
  76. $toEmail = $emailInfo['email'];
  77. $loginInfo = $this->emailTheme['login'];
  78. if (isset($loginInfo['enable']) && $loginInfo['enable']) {
  79. $mailerConfigParam = '';
  80. if (isset($loginInfo['mailerConfig']) && $loginInfo['mailerConfig']) {
  81. $mailerConfigParam = $loginInfo['mailerConfig'];
  82. }
  83. if (isset($loginInfo['widget']) && $loginInfo['widget']) {
  84. $widget = $loginInfo['widget'];
  85. }
  86. if (isset($loginInfo['viewPath']) && $loginInfo['viewPath']) {
  87. $viewPath = $loginInfo['viewPath'];
  88. }
  89. if ($widget && $viewPath) {
  90. list($subject, $htmlBody) = Yii::$service->email->getSubjectAndBody($widget, $viewPath, '', $emailInfo);
  91. $sendInfo = [
  92. 'to' => $toEmail,
  93. 'subject' => $subject,
  94. 'htmlBody' => $htmlBody,
  95. 'senderName'=> Yii::$service->store->currentStore,
  96. ];
  97. Yii::$service->email->send($sendInfo, $mailerConfigParam);
  98. return true;
  99. }
  100. }
  101. }
  102. /**
  103. * @param $emailInfo | Array ,数组格式格式如下:
  104. * [ 'email' => 'xx@xx.com' , [...] ] 其中email是必须有的数组key,对于其他的,
  105. * 可以根据功能添加,添加后,可以在邮件模板的$params中调用,譬如调用email为 $params['email']
  106. * @return boolean , 如果发送成功,则返回true。
  107. * 客户忘记秒发送的邮件
  108. */
  109. public function sendForgotPasswordEmail($emailInfo)
  110. {
  111. $toEmail = $emailInfo['email'];
  112. $forgotPasswordInfo = $this->emailTheme['forgotPassword'];
  113. if (isset($forgotPasswordInfo['enable']) && $forgotPasswordInfo['enable']) {
  114. $mailerConfigParam = '';
  115. if (isset($forgotPasswordInfo['mailerConfig']) && $forgotPasswordInfo['mailerConfig']) {
  116. $mailerConfigParam = $forgotPasswordInfo['mailerConfig'];
  117. }
  118. if (isset($forgotPasswordInfo['widget']) && $forgotPasswordInfo['widget']) {
  119. $widget = $forgotPasswordInfo['widget'];
  120. }
  121. if (isset($forgotPasswordInfo['viewPath']) && $forgotPasswordInfo['viewPath']) {
  122. $viewPath = $forgotPasswordInfo['viewPath'];
  123. }
  124. if ($widget && $viewPath) {
  125. list($subject, $htmlBody) = Yii::$service->email->getSubjectAndBody($widget, $viewPath, '', $emailInfo);
  126. $sendInfo = [
  127. 'to' => $toEmail,
  128. 'subject' => $subject,
  129. 'htmlBody' => $htmlBody,
  130. 'senderName'=> Yii::$service->store->currentStore,
  131. ];
  132. Yii::$service->email->send($sendInfo, $mailerConfigParam);
  133. return true;
  134. }
  135. }
  136. }
  137. /**
  138. * 超时时间:忘记密码发送邮件,内容中的修改密码链接的超时时间。
  139. */
  140. public function getPasswordResetTokenExpire()
  141. {
  142. $forgotPasswordInfo = $this->emailTheme['forgotPassword'];
  143. if (isset($forgotPasswordInfo['passwordResetTokenExpire']) && $forgotPasswordInfo['passwordResetTokenExpire']) {
  144. return $forgotPasswordInfo['passwordResetTokenExpire'];
  145. }
  146. }
  147. /**
  148. * 超时时间: 注册账户激活邮件的token的过去时间
  149. */
  150. public function getRegisterEnableTokenExpire()
  151. {
  152. return $this->registerAccountEnableTokenExpire;
  153. }
  154. /**
  155. * @param $emailInfo | Array ,数组格式格式如下:
  156. * [ 'email' => 'xx@xx.com' , [...] ] 其中email是必须有的数组key,对于其他的,
  157. * 可以根据功能添加,添加后,可以在邮件模板的$params中调用,譬如调用email为 $params['email']
  158. * @return boolean , 如果发送成功,则返回true。
  159. * 客户联系我们邮件。
  160. */
  161. public function sendContactsEmail($emailInfo)
  162. {
  163. $contactsInfo = $this->emailTheme['contacts'];
  164. $toEmail = $contactsInfo['address'];
  165. if (!$toEmail) {
  166. Yii::$service->page->message->addError(['Contact us : receive email is empty , you must config it in email customer contacts email']);
  167. return;
  168. }
  169. if (isset($contactsInfo['enable']) && $contactsInfo['enable']) {
  170. $mailerConfigParam = '';
  171. if (isset($contactsInfo['mailerConfig']) && $contactsInfo['mailerConfig']) {
  172. $mailerConfigParam = $contactsInfo['mailerConfig'];
  173. }
  174. if (isset($contactsInfo['widget']) && $contactsInfo['widget']) {
  175. $widget = $contactsInfo['widget'];
  176. }
  177. if (isset($contactsInfo['viewPath']) && $contactsInfo['viewPath']) {
  178. $viewPath = $contactsInfo['viewPath'];
  179. }
  180. if ($widget && $viewPath) {
  181. list($subject, $htmlBody) = Yii::$service->email->getSubjectAndBody($widget, $viewPath, '', $emailInfo);
  182. $sendInfo = [
  183. 'to' => $toEmail,
  184. 'subject' => $subject,
  185. 'htmlBody' => $htmlBody,
  186. 'senderName'=> Yii::$service->store->currentStore,
  187. ];
  188. Yii::$service->email->send($sendInfo, $mailerConfigParam);
  189. return true;
  190. }
  191. }
  192. }
  193. /**
  194. * @param $emailInfo | Array ,数组格式格式如下:
  195. * [ 'email' => 'xx@xx.com' , [...] ] 其中email是必须有的数组key,对于其他的,
  196. * 可以根据功能添加,添加后,可以在邮件模板的$params中调用,譬如调用email为 $params['email']
  197. * @return boolean , 如果发送成功,则返回true。
  198. * 订阅邮件成功邮件
  199. */
  200. public function sendNewsletterSubscribeEmail($emailInfo)
  201. {
  202. $toEmail = $emailInfo['email'];
  203. $newsletterInfo = $this->emailTheme['newsletter'];
  204. if (isset($newsletterInfo['enable']) && $newsletterInfo['enable']) {
  205. $mailerConfigParam = '';
  206. if (isset($newsletterInfo['mailerConfig']) && $newsletterInfo['mailerConfig']) {
  207. $mailerConfigParam = $newsletterInfo['mailerConfig'];
  208. }
  209. if (isset($newsletterInfo['widget']) && $newsletterInfo['widget']) {
  210. $widget = $newsletterInfo['widget'];
  211. }
  212. if (isset($newsletterInfo['viewPath']) && $newsletterInfo['viewPath']) {
  213. $viewPath = $newsletterInfo['viewPath'];
  214. }
  215. if ($widget && $viewPath) {
  216. list($subject, $htmlBody) = Yii::$service->email->getSubjectAndBody($widget, $viewPath, '', $emailInfo);
  217. $sendInfo = [
  218. 'to' => $toEmail,
  219. 'subject' => $subject,
  220. 'htmlBody' => $htmlBody,
  221. 'senderName'=> Yii::$service->store->currentStore,
  222. ];
  223. Yii::$service->email->send($sendInfo, $mailerConfigParam);
  224. return true;
  225. }
  226. }
  227. }
  228. }