EmailController.php
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace App\Http\Controllers\Bside\Subscribe;
use App\Enums\Common\Code;
use App\Helper\Arr;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Inquiry\InquiryLogic;
use App\Models\Inquiry\InquiryForm;
use App\Models\Subscribe\Email;
use App\Models\Subscribe\GroupSendTask;
use App\Models\Subscribe\Smtp;
use App\Rules\Ids;
use App\Services\BatchExportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
/**
* 订阅邮件管理
* Class EmailController
* @package App\Http\Controllers\Bside\Subscribe
* @author zbj
* @date 2024/8/29
*/
class EmailController extends BaseController
{
public function list(Email $emailModel)
{
if(!empty($this->param['email'])){
$this->map['email'] = ['like','%'.$this->param['email'].'%'];
}
$lists = $emailModel->lists($this->map, $this->page, $this->row);
$this->response('success', Code::SUCCESS, $lists);
}
public function delete(Email $emailModel)
{
$this->request->validate([
'id' => 'required',
], [
'id.required' => 'id不为空',
]);
$emailModel->del(['id' => $this->param['id']]);
$this->response('success');
}
public function export(Email $emailModel)
{
$data = $emailModel->list([], 'id', ['*'], 'desc', 1000);
//生成文件,发送到客户端
$map = [
'email' => '邮箱',
'created_at' => '订阅时间',
];
$table = new BatchExportService("订阅邮箱导出");
$file = $table->head($map)->data($data)->save();
if (!$file) {
throw new \Exception('文件生成失败,请重试');
}
$fileurl = Storage::disk('runtime')->url($file);
$this->response('success', Code::SUCCESS, ['url' => $fileurl]);
}
public function set_smtp(Smtp $smtp)
{
$this->request->validate([
'email' => ['required', 'email', 'max:200'],
'password' => ['required', 'max:200'],
'host' => ['required', 'max:200', 'regex:/[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/'],
'from_name' => ['required', 'max:200'],
], [
'email.required' => '邮箱必须',
'email.email' => '邮箱格式错误',
'password.required' => '授权码/密码必须',
'host.required' => 'smtp地址必须',
'host.regex' => 'smtp格式错误',
'from_name.required' => '发信人昵称必须',
]);
$this->param['project_id'] = $this->project['id'];
$info = $smtp->read(['project_id' => $this->project['id']]);
if (!$info) {
$smtp->add($this->param);
} else {
$smtp->edit($this->param, ['project_id' => $this->project['id']]);
}
$this->response('success');
}
public function get_smtp(Smtp $smtp){
$info = $smtp->read(['project_id' => $this->project['id']]);
$this->response($info??[]);
}
public function group_send(GroupSendTask $groupSendTask)
{
$this->request->validate([
'project_id' => ['required'],
'emails' => ['required'],
'subject' => ['required'],
'text' => ['required'],
], [
'project_id.required' => '参数异常',
'emails.required' => '发送对象必须',
'subject.required' => '邮件主题必须',
'host.required' => 'smtp地址必须',
]);
if($this->param['send_time'] && !strtotime($this->param['send_time'])){
$this->fail('发送时间格式错误',Code::USER_ERROR);
}
$smtp = new Smtp();
$info = $smtp->read(['project_id' => $this->param['project_id']]);
if(!$info){
$this->fail('请先设置SMTP',Code::USER_ERROR);
}
$this->param['emails'] = Arr::a2s($this->param['emails']);
$this->param['send_time'] = empty($this->param['send_time']) ? 0 : strtotime($this->param['send_time']);
$this->param['user_id'] = $this->user['id'];
$groupSendTask->add($this->param);
$this->response('success');
}
}