Emailconfig.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use think\Db;
  11. class Emailconfig extends Permissions
  12. {
  13. public function index()
  14. {
  15. $data = \app\common\model\Emailconfig::get(1);
  16. $this->assign('data', $data);
  17. $grouptabs = (new \app\common\model\ConfigTab())->order('sort desc')->select();
  18. $this->assign('tabs', $grouptabs);
  19. return $this->fetch();
  20. }
  21. public function publish()
  22. {
  23. if ($this->request->isPost()) {
  24. $post = $this->request->post();
  25. $validate = new \think\Validate([
  26. ['from_email', 'require|email', '发件箱不能为空|发件箱格式不正确'],
  27. ['from_name', 'require', '发件人不能为空'],
  28. ['smtp', 'require', '邮箱smtp服务器不能为空'],
  29. ['username', 'require|email', '登录账户不能为空|登录账户应为邮箱格式'],
  30. ['password', 'require', '登录密码不能为空'],
  31. ['content', 'require', '邮件模板不能为空'],
  32. ]);
  33. if (!$validate->check($post)) {
  34. $this->error('提交失败:' . $validate->getError());
  35. }
  36. if (false == Db::name('emailconfig')->where('email', 'email')->update($post)) {
  37. $this->error('提交失败');
  38. } else {
  39. $this->success('提交成功', 'admin/emailconfig/index');
  40. }
  41. } else {
  42. $this->error('非法请求');
  43. }
  44. }
  45. //测试发送
  46. public function mailto()
  47. {
  48. if ($this->request->isPost()) {
  49. $post = $this->request->post();
  50. $validate = new \think\Validate([
  51. ['email', 'require|email', '收件箱不能为空|收件箱格式不正确'],
  52. ]);
  53. if (!$validate->check($post)) {
  54. $this->error('提交失败:' . $validate->getError());
  55. }
  56. $to_email = $post['email'];
  57. $data = \app\common\model\Emailconfig::get(1);
  58. $title = $data['title'];
  59. $content = str_replace(['{nickname}', '{code}', '{passport}', '{email}', '{time}'], ['tplay1', '1234', 'tplay2', 'tplay@test.com', date('Y-m-d H:i:s', time())], $data['content']);
  60. $from_email = $data['from_email'];
  61. $from_name = $data['from_name'];
  62. $smtp = $data['smtp'];
  63. $username = $data['username'];
  64. $password = $data['password'];
  65. if (!$title || !$content || !$from_email || !$from_name || !$smtp || !$username || !$password) {
  66. $this->error('请先配置发件邮箱信息');
  67. }
  68. $mailto = SendMail($to_email, $title, $content, $from_email, $from_name, $smtp, $username, $password);
  69. if (false == $mailto) {
  70. $this->error('发送失败');
  71. } else {
  72. $this->success('邮件发送成功');
  73. }
  74. } else {
  75. return $this->fetch();
  76. }
  77. }
  78. }