Email.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace addons\shopro\channel;
  3. use addons\shopro\notification\Notification;
  4. use think\Validate;
  5. use app\common\library\Email as SendEmail;
  6. class Email
  7. {
  8. public function __construct()
  9. {
  10. }
  11. /**
  12. * 发送 微信模板消息
  13. *
  14. * @param mixed $notifiable // 通知用户
  15. * @param 通知内容
  16. * @return void
  17. */
  18. public function send($notifiable, Notification $notification)
  19. {
  20. $data = [];
  21. if (method_exists($notification, 'toEmail')) {
  22. $data = $notification->toEmail($notifiable);
  23. if ($data && isset($notifiable['email']) && Validate::is($notifiable['email'], "email")) {
  24. try {
  25. $email = new SendEmail;
  26. $result = $email
  27. ->to($notifiable['email'], $notifiable['nickname'])
  28. ->subject(($data['data'] ? $data['data']['template'] : '邮件通知'))
  29. ->message('<div style="min-height:550px; padding: 50px 20px 100px;">' . $data['content'] . '</div>')
  30. ->send();
  31. if ($result) {
  32. // 发送成功
  33. $notification->sendOk('Email');
  34. } else {
  35. // 邮件发送失败
  36. \think\Log::error('邮件消息发送失败:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($email->getError()));
  37. }
  38. } catch (\Exception $e) {
  39. // 因为配置较麻烦,这里捕获异常防止因为缺少字段,导致队列一直执行不成功
  40. format_log_error($e, 'email_notification', '用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
  41. }
  42. return true;
  43. }
  44. // 没有openid
  45. \think\Log::error('邮件消息发送失败,没有 email,或 email 格式不正确:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
  46. }
  47. return true;
  48. }
  49. }