MailNotify.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Modules\Message;
  3. use App\Consts\SysConsts;
  4. use Exception;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 邮件通知
  9. */
  10. class MailNotify
  11. {
  12. /**
  13. * code
  14. */
  15. private $project_code = SysConsts::PROJECT_CODE;
  16. /**
  17. * key
  18. */
  19. private $project_key = SysConsts::PROJECT_KEY;
  20. /**
  21. * 通知地址
  22. */
  23. private $url = SysConsts::MESSAGE_NOTIFY_BASE_URI;
  24. /**
  25. * 收件人
  26. */
  27. private $to_emails;
  28. /**
  29. * 抄送
  30. */
  31. private $cc_emails;
  32. /**
  33. * 主题
  34. */
  35. private $subject;
  36. /**
  37. * 邮件内容
  38. */
  39. private $body;
  40. /**
  41. * 邮件附件
  42. */
  43. private $attaches;
  44. /**
  45. * 时间戳
  46. */
  47. private $timestamps;
  48. /**
  49. * 通知方式
  50. */
  51. private $method;
  52. /**
  53. * 请求路由
  54. */
  55. private $path;
  56. public function __construct(array $data)
  57. {
  58. $this->to_emails = $data['to_emails'];
  59. $this->cc_emails = isset($data['cc_emails']) ? $data['cc_emails'] : '';
  60. $this->subject = $data['subject'];
  61. $this->body = $data['body'];
  62. $this->delay_times = isset($data['delay_times']) ? $data['delay_times'] : 0;
  63. $this->method = 'POST';
  64. $this->path = 'send_raw_email';
  65. $this->timestamps = time();
  66. }
  67. /**
  68. * 添加附件
  69. */
  70. public function addAttach(string $file_path, string $file_name = '')
  71. {
  72. if (!$this->attaches) {
  73. $this->attaches = [];
  74. }
  75. if (!$file_name) {
  76. preg_match('/([0-9a-zA-Z\x{4e00}-\x{9fa5}]+)\.?\w*$/u', $file_path, $mathes);
  77. $file_name = $mathes[0];
  78. }
  79. $this->attaches[] = ['file_name' => $file_name, 'file_path' => $file_path];
  80. }
  81. /**
  82. * 生成签名
  83. */
  84. private function makeSign()
  85. {
  86. return md5($this->project_code . $this->timestamps . $this->project_key);
  87. }
  88. /**
  89. * 设置请求参数
  90. */
  91. private function setRequestOptions()
  92. {
  93. $project_code = $this->project_code;
  94. $timestamp = $this->timestamps;
  95. $sign = $this->makeSign();
  96. $to_email = $this->to_emails;
  97. $cc_email = $this->cc_emails;
  98. $subject = $this->subject;
  99. $body = $this->body;
  100. $delay_times = $this->delay_times;
  101. $params = compact('project_code', 'timestamp', 'sign', 'to_email', 'cc_email', 'subject', 'body', 'delay_times');
  102. if ($this->attaches) {
  103. $mults = collect($params)->map(function ($item, $k) {
  104. return ['name' => $k, 'contents' => $item];
  105. })->all();
  106. foreach ($this->attaches as $attach) {
  107. $mults[] = [
  108. 'name' => str_replace('.', '_', $attach['file_name']),
  109. 'contents' => fopen(storage_path($attach['file_path']), 'r+'),
  110. 'filename' => $attach['file_name'],
  111. ];
  112. }
  113. return ['multipart' => $mults];
  114. } else {
  115. return ['form_params' => $params];
  116. }
  117. }
  118. /**
  119. * 发送消息通知
  120. */
  121. public function notify()
  122. {
  123. $client = new Client(['base_uri' => $this->url, 'timesout' => 10]);
  124. try {
  125. $options = $this->setRequestOptions();
  126. $response = $client->request($this->method, $this->path, $options);
  127. $contents = $response->getBody()->getContents();
  128. if ($contents) {
  129. $result = json_decode($contents);
  130. if ($result->code === 0) {
  131. return true;
  132. } else {
  133. Log::error('email_notify: ' . $contents);
  134. }
  135. } else {
  136. Log::error('email_notify: no response');
  137. }
  138. } catch (Exception $e) {
  139. Log::error('email_notify: ' . $e->getMessage());
  140. }
  141. }
  142. }