MailerController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use cmf\controller\AdminBaseController;
  13. use think\Validate;
  14. class MailerController extends AdminBaseController
  15. {
  16. /**
  17. * 邮箱配置
  18. * @adminMenu(
  19. * 'name' => '邮箱配置',
  20. * 'parent' => 'admin/Setting/default',
  21. * 'display'=> true,
  22. * 'hasView'=> true,
  23. * 'order' => 10,
  24. * 'icon' => '',
  25. * 'remark' => '邮箱配置',
  26. * 'param' => ''
  27. * )
  28. */
  29. public function index()
  30. {
  31. $emailSetting = cmf_get_option('smtp_setting');
  32. $this->assign($emailSetting);
  33. return $this->fetch();
  34. }
  35. /**
  36. * 邮箱配置
  37. * @adminMenu(
  38. * 'name' => '邮箱配置提交保存',
  39. * 'parent' => 'index',
  40. * 'display'=> false,
  41. * 'hasView'=> false,
  42. * 'order' => 10000,
  43. * 'icon' => '',
  44. * 'remark' => '邮箱配置提交保存',
  45. * 'param' => ''
  46. * )
  47. */
  48. public function indexPost()
  49. {
  50. if ($this->request->isPost()) {
  51. $post = array_map('trim', $this->request->param());
  52. if (in_array('', $post) && !empty($post['smtpsecure'])) {
  53. $this->error("不能留空!");
  54. }
  55. cmf_set_option('smtp_setting', $post);
  56. $this->success("保存成功!");
  57. }
  58. }
  59. /**
  60. * 邮件模板
  61. * @adminMenu(
  62. * 'name' => '邮件模板',
  63. * 'parent' => 'index',
  64. * 'display'=> false,
  65. * 'hasView'=> true,
  66. * 'order' => 10000,
  67. * 'icon' => '',
  68. * 'remark' => '邮件模板',
  69. * 'param' => ''
  70. * )
  71. */
  72. public function template()
  73. {
  74. $allowedTemplateKeys = ['verification_code'];
  75. $templateKey = $this->request->param('template_key');
  76. if (empty($templateKey) || !in_array($templateKey, $allowedTemplateKeys)) {
  77. $this->error('非法请求!');
  78. }
  79. $template = cmf_get_option('email_template_' . $templateKey);
  80. $this->assign($template);
  81. return $this->fetch('template_verification_code');
  82. }
  83. /**
  84. * 邮件模板提交
  85. * @adminMenu(
  86. * 'name' => '邮件模板提交',
  87. * 'parent' => 'index',
  88. * 'display'=> false,
  89. * 'hasView'=> false,
  90. * 'order' => 10000,
  91. * 'icon' => '',
  92. * 'remark' => '邮件模板提交',
  93. * 'param' => ''
  94. * )
  95. */
  96. public function templatePost()
  97. {
  98. if ($this->request->isPost()) {
  99. $allowedTemplateKeys = ['verification_code'];
  100. $templateKey = $this->request->param('template_key');
  101. if (empty($templateKey) || !in_array($templateKey, $allowedTemplateKeys)) {
  102. $this->error('非法请求!');
  103. }
  104. $data = $this->request->param();
  105. unset($data['template_key']);
  106. cmf_set_option('email_template_' . $templateKey, $data);
  107. $this->success("保存成功!");
  108. }
  109. }
  110. /**
  111. * 邮件发送测试
  112. * @adminMenu(
  113. * 'name' => '邮件发送测试',
  114. * 'parent' => 'index',
  115. * 'display'=> false,
  116. * 'hasView'=> true,
  117. * 'order' => 10000,
  118. * 'icon' => '',
  119. * 'remark' => '邮件发送测试',
  120. * 'param' => ''
  121. * )
  122. */
  123. public function test()
  124. {
  125. if ($this->request->isPost()) {
  126. $validate = new Validate();
  127. $validate->rule([
  128. 'to' => 'require|email',
  129. 'subject' => 'require',
  130. 'content' => 'require',
  131. ]);
  132. $validate->message([
  133. 'to.require' => '收件箱不能为空!',
  134. 'to.email' => '收件箱格式不正确!',
  135. 'subject.require' => '标题不能为空!',
  136. 'content.require' => '内容不能为空!',
  137. ]);
  138. $data = $this->request->param();
  139. if (!$validate->check($data)) {
  140. $this->error($validate->getError());
  141. }
  142. $result = cmf_send_email($data['to'], $data['subject'], $data['content']);
  143. if ($result && empty($result['error'])) {
  144. $this->success('发送成功!');
  145. } else {
  146. $this->error('发送失败:' . $result['message']);
  147. }
  148. } else {
  149. return $this->fetch();
  150. }
  151. }
  152. }