Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SmtpmailLogic.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\user\logic;
  14. use think\Model;
  15. use think\Db;
  16. use think\Request;
  17. use think\Config;
  18. /**
  19. * 邮箱逻辑定义
  20. * Class CatsLogic
  21. * @package user\Logic
  22. */
  23. class SmtpmailLogic extends Model
  24. {
  25. private $home_lang = 'cn';
  26. /**
  27. * 初始化操作
  28. */
  29. public function initialize() {
  30. parent::initialize();
  31. $this->home_lang = get_home_lang();
  32. }
  33. /**
  34. * 发送邮件
  35. */
  36. public function send_email($email = '', $title = '', $type = 'reg', $scene = 2, $data = [])
  37. {
  38. // 是否传入邮箱地址
  39. $email = trim($email);
  40. if (empty($email)) {
  41. return ['code'=>0, 'msg'=>"邮箱地址参数不能为空!"];
  42. } else {
  43. $email_arr = explode(',', $email);
  44. if (1 == count($email_arr)) {
  45. if (!check_email($email)) {
  46. return ['code'=>0, 'msg'=>"邮箱格式不正确!"];
  47. }
  48. } else {
  49. foreach ($email_arr as $key => $val) {
  50. $val = trim($val);
  51. if (!check_email($val) || empty($val)) {
  52. unset($email_arr[$key]);
  53. }
  54. }
  55. if (empty($email_arr)) {
  56. return ['code'=>0, 'msg'=>"邮箱格式不正确!"];
  57. }
  58. $email = implode(',', $email_arr);
  59. }
  60. }
  61. // 查询扩展是否开启
  62. $openssl_funcs = get_extension_funcs('openssl');
  63. if (!$openssl_funcs) {
  64. return ['code'=>0, 'msg'=>"请联系空间商,开启php的 <font color='red'>openssl</font> 扩展!"];
  65. }
  66. // 是否填写邮件配置
  67. $smtp_config = tpCache('smtp');
  68. if (empty($smtp_config['smtp_user']) || empty($smtp_config['smtp_pwd'])) {
  69. return ['code'=>0, 'msg'=>"该功能待开放,网站管理员尚未完善邮件配置!"];
  70. }
  71. // 邮件使用场景
  72. $scene = intval($scene);
  73. $send_email_scene = config('send_email_scene');
  74. $send_scene = $send_email_scene[$scene]['scene'];
  75. // 获取邮件模板
  76. $emailtemp = M('smtp_tpl')->where(['send_scene' => $send_scene, 'lang' => $this->home_lang])->find();
  77. // 是否启用邮件模板
  78. if (empty($emailtemp) || empty($emailtemp['is_open'])) {
  79. return ['code'=>0, 'msg'=>"该功能待开放,网站管理员尚未启用(<font color='red'>{$emailtemp['tpl_name']}</font>)邮件模板"];
  80. }
  81. // 会员ID
  82. $users_id = session('users_id');
  83. // 找回密码
  84. if ('retrieve_password' == $type) {
  85. // 判断邮箱是否存在
  86. $where = array(
  87. 'info' => array('eq',$email),
  88. 'lang' => array('eq',$this->home_lang),
  89. );
  90. $users_list = M('users_list')->where($where)->field('users_id,info')->find();
  91. // 判断会员是否已绑定邮箱
  92. $userswhere = array(
  93. 'email' => array('eq',$email),
  94. 'lang' => array('eq',$this->home_lang),
  95. );
  96. $usersdata = M('users')->where($userswhere)->field('is_email,is_activation')->find();
  97. if (!empty($usersdata)) {
  98. if (empty($usersdata['is_activation'])) {
  99. return ['code'=>0, 'msg'=>'该会员尚未激活,不能找回密码!'];
  100. } else if (empty($usersdata['is_email'])) {
  101. return ['code'=>0, 'msg'=>'邮箱地址未绑定,不能找回密码!'];
  102. }
  103. }
  104. if (!empty($users_list)) {
  105. $time = getTime();
  106. // 数据添加
  107. $datas['source'] = 4; // 来源,与场景ID对应:4=找回密码
  108. $datas['email'] = $email;
  109. $datas['users_id'] = $users_list['users_id'];
  110. $datas['code'] = rand(1000,9999);
  111. $datas['lang'] = $this->home_lang;
  112. $datas['add_time'] = $time;
  113. M('smtp_record')->add($datas);
  114. } else {
  115. return ['code'=>0, 'msg'=>'邮箱地址不存在!'];
  116. }
  117. }
  118. // 邮箱绑定
  119. else if ('bind_email' == $type) {
  120. // 判断邮箱是否已存在
  121. $listwhere = array(
  122. 'info' => array('eq',$email),
  123. 'users_id' => array('neq',$users_id),
  124. 'lang' => array('eq',$this->home_lang),
  125. );
  126. $users_list = M('users_list')->where($listwhere)->field('info')->find();
  127. // 判断会员是否已绑定相同邮箱
  128. $userswhere = array(
  129. 'users_id' => array('eq',$users_id),
  130. 'email' => array('eq',$email),
  131. 'is_email' => 1,
  132. 'lang' => array('eq',$this->home_lang),
  133. );
  134. $usersdata = M('users')->where($userswhere)->field('is_email')->find();
  135. if (!empty($usersdata['is_email'])) {
  136. return ['code'=>0, 'msg'=>'邮箱已绑定,无需重新绑定!'];
  137. }
  138. // 邮箱数据处理
  139. if (empty($users_list)) {
  140. $time = getTime();
  141. // 数据添加
  142. $datas['source'] = 3; // 来源,与场景ID对应:3=绑定邮箱
  143. $datas['email'] = $email;
  144. $datas['users_id'] = $users_id;
  145. $datas['code'] = rand(1000,9999);
  146. $datas['lang'] = $this->home_lang;
  147. $datas['add_time'] = $time;
  148. M('smtp_record')->add($datas);
  149. } else {
  150. return ['code'=>0, 'msg'=>"邮箱已经存在,不可以绑定!"];
  151. }
  152. }
  153. // 会员注册
  154. else if ('reg' == $type) {
  155. // 判断邮箱是否已存在
  156. $where = array(
  157. 'info' => array('eq',$email),
  158. 'lang' => array('eq',$this->home_lang),
  159. );
  160. $users_list = M('users_list')->where($where)->field('info')->find();
  161. if (empty($users_list)) {
  162. $time = getTime();
  163. // 数据添加
  164. $datas['source'] = 2; // 来源,与场景ID对应:2=注册
  165. $datas['email'] = $email;
  166. $datas['code'] = rand(1000,9999);
  167. $datas['lang'] = $this->home_lang;
  168. $datas['add_time'] = $time;
  169. M('smtp_record')->add($datas);
  170. } else {
  171. return ['code'=>0, 'msg'=>'邮箱已存在!'];
  172. }
  173. }
  174. // 订单(支付、发货)提醒
  175. else if ('order_msg' == $type) {
  176. $content = '订单有新的消息,请登录查看。';
  177. if (!empty($data)) {
  178. $PayMethod = '';
  179. if (!empty($data['pay_method'])) {
  180. switch ($data['pay_method']) {
  181. case 'balance':
  182. $PayMethod = '余额支付';
  183. break;
  184. case 'delivery_pay':
  185. $PayMethod = '货到付款';
  186. break;
  187. case 'wechat':
  188. $PayMethod = '微信';
  189. break;
  190. case 'alipay':
  191. $PayMethod = '支付宝';
  192. break;
  193. default:
  194. $PayMethod = '第三方支付';
  195. break;
  196. }
  197. }
  198. switch ($data['type']) {
  199. case '1':
  200. $content = '您好,管理员。 会员(' . $data['nickname'] . ')使用'. $PayMethod .'对订单(' . $data['order_code'] . ')支付完成,请登录后台审查并及时发货。<br/>';
  201. $order_data = Db::name('shop_order')->where('order_code',$data['order_code'])->field('order_id,order_total_amount')->find();
  202. $order_detail = Db::name('shop_order_details')
  203. ->where('order_id',$order_data['order_id'])
  204. ->select();
  205. foreach ($order_detail as $k => $v) {
  206. $content .= "{$v['product_name']} ¥".floatval($v['product_price'])." x {$v['num']} = ¥".floatval($v['product_price']*$v['num']) .'<br/>';
  207. }
  208. $content .= "订单总额:¥" . floatval($order_data['order_total_amount']);
  209. break;
  210. case '2':
  211. $url = request()->domain() . url('user/Shop/shop_order_details', ['order_id'=>$data['order_id']]);
  212. $chayue = '<a href="'. $url .'">查阅</a>';
  213. $content = '您好,' . $data['nickname'] . '。 管理员已对订单(' . $data['order_code'] . ')发货完成,请登录会员中心'. $chayue .'。';
  214. break;
  215. }
  216. }
  217. }
  218. // 会员投稿提醒
  219. else if ('usersRelease' == $type) {
  220. $content = !empty($emailtemp['tpl_title']) ? $emailtemp['tpl_title'] : '您有新的投稿文档,请及时查看!';
  221. if (!empty($data)) {
  222. $content .= '<br/>文档标题:' . $data['title'] . '<br/>文档内容:' . $data['content'] . '<br/>投稿时间:' . $data['add_time'] . '<br/>文档审核:' . $data['arcrank'];
  223. }
  224. }
  225. // 判断标题拼接
  226. $title = addslashes($title);
  227. $web_name = $emailtemp['tpl_name'].':'.$title.'-'.tpCache('web.web_name');
  228. $content = !empty($content) ? $content : '感谢您的注册,您的邮箱验证码为: '.$datas['code'];
  229. $html = "<p style='text-align: left;'>{$web_name}</p><p style='text-align: left;'>{$content}</p>";
  230. if (isMobile()) {
  231. $html .= "<p style='text-align: left;'>——来源:移动端</p>";
  232. } else {
  233. $html .= "<p style='text-align: left;'>——来源:电脑端</p>";
  234. }
  235. // 实例化类库,调用发送邮件
  236. $res = send_email($email, $emailtemp['tpl_title'], $html, $send_scene);
  237. if (intval($res['code']) == 1) {
  238. return ['code'=>1, 'msg'=>$res['msg']];
  239. } else {
  240. return ['code'=>0, 'msg'=>$res['msg']];
  241. }
  242. }
  243. }