Email.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\message;
  13. use PHPMailer\PHPMailer\PHPMailer;
  14. use PHPMailer\PHPMailer\Exception;
  15. use app\model\system\Config as ConfigModel;
  16. use app\model\BaseModel;
  17. /**
  18. * 邮件管理类
  19. */
  20. class Email extends BaseModel
  21. {
  22. /********************************************************************* 邮件发送配置 start *********************************************************************************/
  23. /**
  24. * 设置邮件发送配置
  25. * array $data
  26. */
  27. public function setEmailConfig($data, $is_use)
  28. {
  29. $config = new ConfigModel();
  30. $res = $config->setConfig($data, '邮件配置', $is_use, [['site_id', '=', 0], ['app_module', '=', 'admin'], ['config_key', '=', 'EMAIL_CONFIG']]);
  31. return $res;
  32. }
  33. /**
  34. * 获取邮件发送配置
  35. */
  36. public function getEmailConfig()
  37. {
  38. $config = new ConfigModel();
  39. $res = $config->getConfig([['site_id', '=', 0], ['app_module', '=', 'admin'], ['config_key', '=', 'EMAIL_CONFIG']]);
  40. return $res;
  41. }
  42. /********************************************************************* 邮件发送配置 end *********************************************************************************/
  43. /********************************************************************* 邮件发送记录 start *********************************************************************************/
  44. /**
  45. * 添加邮件记录
  46. * @param $data
  47. */
  48. public function addEmailRecords($data){
  49. $res = model("message_email_records")->add($data);
  50. if ($res === false) {
  51. return $this->error('', 'UNKNOW_ERROR');
  52. }
  53. return $res;
  54. }
  55. /**
  56. * 邮件记录编辑
  57. * @param $data
  58. * @param $condition
  59. */
  60. public function editEmailRecords($data, $condition){
  61. $res = model("message_email_records")->update($data, $condition);
  62. if ($res === false) {
  63. return $this->error('', 'UNKNOW_ERROR');
  64. }
  65. return $res;
  66. }
  67. /**
  68. * 删除邮箱记录
  69. */
  70. public function deleteEmailRecords($condition){
  71. $res = model("message_email_records")->delete($condition);
  72. if ($res === false) {
  73. return $this->error('', 'UNKNOW_ERROR');
  74. }
  75. return $res;
  76. }
  77. /**
  78. * 邮箱记录分页列表
  79. * @param array $condition
  80. * @param int $page
  81. * @param int $page_size
  82. * @param string $order
  83. * @param string $field
  84. * @return \multitype
  85. */
  86. public function getEmailRecordsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc', $field = '*')
  87. {
  88. $list = model('message_email_records')->pageList($condition, $field, $order, $page, $page_size);
  89. return $this->success($list);
  90. }
  91. /********************************************************************* 邮件发送记录 end *********************************************************************************/
  92. /********************************************************************* 邮件发送 start *********************************************************************************/
  93. /**
  94. * 发送消息
  95. * @param array $data
  96. */
  97. public function sendMessage($data = []){
  98. $support_type = $data["support_type"] ?? [];
  99. //验证是否支持邮箱发送
  100. if(!empty($support_type) && !in_array("email", $support_type))
  101. return $this->success();
  102. $message_info = $data["message_info"];
  103. //邮箱是否开启
  104. if($message_info["email_is_open"] == 0)
  105. return $this->error();
  106. //消息内容 变量替换
  107. $message_info = $data["message_info"];
  108. $title = $message_info["email_title"];//邮箱发送标题
  109. $content = $message_info["email_content"];//邮箱发送内容
  110. if(empty($title) || empty($content)){
  111. // return $this->error([], "发送邮件时,标题或内容不可为空!");
  112. $return_result = $this->error([], "EMAIL_FAIL");
  113. }
  114. $var_parse = $data["var_parse"];
  115. //循环替换变量解析
  116. foreach($message_info["message_json_array"] as $k => $v){
  117. if(!empty($var_parse[$k])){
  118. $content = str_replace("{".$v."}", $var_parse[$k], $content);
  119. $title = str_replace("{".$v."}", $var_parse[$k], $title);
  120. }
  121. }
  122. $data["subject"] = $title;
  123. $data["body"] = $content;
  124. $config_result = $this->getEmailConfig();
  125. $config = $config_result["data"]["value"];
  126. $data["host"] = $config["host"];
  127. $data["port"] = $config["port"];
  128. $data["username"] = $config["username"];
  129. $data["password"] = $config["password"];
  130. $data["from"] = $config["from"];
  131. $data["from_name"] = $data["site_name"];
  132. $data["address"] = $data["email_account"];
  133. $data["attachment"] = $data["attachment"] ?? '' ;
  134. $data["attachment_name"] = $data["attachment_name"] ?? '' ;
  135. $result = $this->send($data);
  136. //增加发送邮件记录
  137. $status = $result["code"] >= 0 ? 1 : 0;
  138. $send_time = '';
  139. $message_result = "发送成功";
  140. if($result["code"] >= 0){
  141. $send_time = time();
  142. $return_result = $this->success([], "EMAIL_SUCCESS");
  143. }else{
  144. $message_result = $result["message"];
  145. $return_result = $this->error([], "EMAIL_FAIL");
  146. }
  147. $records_data = array(
  148. "account" => $data["email_account"],
  149. "status" => $status,
  150. "title" => $title,
  151. "content" => $content,
  152. "keywords" => $data["keywords"],
  153. "create_time" => time(),
  154. "send_time" => $send_time,
  155. "result" => $message_result,
  156. "keywords_name" => $message_info["title"]
  157. );
  158. $this->addEmailRecords($records_data);
  159. return $return_result;
  160. }
  161. /**
  162. * 邮件发送
  163. * @param $param
  164. */
  165. public function send($param = []){
  166. // return $this->success();
  167. $mail = new PHPMailer(true);
  168. $host = $param["host"];// 链接域名邮箱的服务器地址
  169. $username = $param["username"];// smtp登录的账号
  170. $password = $param["password"];// smtp登录的密码 使用生成的授权码
  171. $port = $param["port"];// 设置ssl连接smtp服务器的远程服务器端口号
  172. $from = $param["from"];// 设置发件人邮箱地址 同登录账号
  173. $from_name = $param["from_name"];//设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名
  174. $address = $param["address"];// 添加多个收件人 则多次调用方法即可
  175. $attachment = $param["attachment"];// 为该邮件添加附件
  176. $attachment_name = $param["attachment_name"];// 为该邮件添加附件重命名
  177. $is_html = true;// 邮件正文是否为html编码 注意此处是一个方法
  178. $subject = $param["subject"];//标题
  179. $body = $param["body"];// 添加邮件正文
  180. try {
  181. //Server settings
  182. // $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output 设置使用ssl加密方式登录鉴权
  183. $mail->isSMTP(); // Send using SMTP
  184. $mail->Host = $host ?? 'smtp1.example.com'; // Set the SMTP server to send through
  185. $mail->SMTPAuth = true; //smtp需要鉴权 这个必须是true Enable SMTP authentication
  186. $mail->Username = $username ?? 'user@example.com'; // SMTP username
  187. $mail->Password = $password ?? 'secret'; // SMTP password
  188. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // 设置使用ssl加密方式登录鉴权 Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
  189. $mail->Port = $port ?? 465; // TCP port to connect to
  190. $mail->CharSet= 'UTF-8';//指定字符集
  191. //Recipients
  192. $mail->setFrom($from, $from_name);
  193. // $mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
  194. $mail->addAddress($address); // Name is optional
  195. // $mail->addReplyTo('info@example.com', 'Information');
  196. // $mail->addCC('cc@example.com');
  197. // $mail->addBCC('bcc@example.com');
  198. // 附件 Attachments 判断附件是否真实存在
  199. if(!empty($attachment) && is_file($attachment)){
  200. if(!empty($attachment_name)){
  201. $mail->addAttachment($attachment, $attachment_name); // Add attachments 附件 重命名
  202. }else{
  203. $mail->addAttachment($attachment); // Add attachments 附件
  204. }
  205. }
  206. // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
  207. // Content
  208. $mail->isHTML($is_html); // Set email format to HTML
  209. // $mail->Subject = mbStrreplace($subject);
  210. // $mail->Body = mbStrreplace($body);
  211. $mail->Subject = $subject;
  212. $mail->Body = $body;
  213. // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  214. $mail->send();
  215. return $this->success(1);
  216. } catch (Exception $e) {
  217. return $this->error("", $mail->ErrorInfo);
  218. }
  219. }
  220. /********************************************************************* 邮件发送 end *********************************************************************************/
  221. }