MailApi.php
2.1 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
<?php
namespace App\Http\Controllers;
use App\Models\Email;
use App\Models\Host;
use Helper\Fun;
use Helper\Mail\Imap;
/**
* 提供邮件各项数据
* @author:dc
* @time 2023/2/4 11:18
* Class MailApi
* @package App\Http\Controllers
*/
class MailApi
{
/**
* 添加新的邮箱
* @author:dc
* @time 2023/2/4 15:37
*/
public function login(){
// $mail,$password,$imap,$smtp
$formData = request()->only(['email','password','imap','smtp']);
$validator = validator($formData,[
'email' => ['required','email'],
'password' => ['required','min:8','max:32'],
'imap' => ['required'],
'smtp' => ['required'],
],[
]);
if($validator->fails()){
Fun::response()
->message($validator->errors()->first())
->status(400)
->throw();
}
// host
$model = Email::_first($formData['email']);
if(!$model){
$model = new Email();
$model->email = $formData['email'];
}
$model->imap = $formData['imap'];
$model->smtp = $formData['smtp'];
$model->password = encrypt($formData['password']);
$imap = new Imap();
// 是否初始成功
try {
$imap->login("ssl://{$formData['imap']}:993",$model->email,$model->password);
}catch (\Throwable $e){
Fun::response()
->message($e->getMessage())
->status(400)
->throw();
}
// 登录成功了,密码验证字段通过
$model->pass_error = 0;
// 保存好邮箱
$model->save();
// 开始同步文件夹
$folder = $imap->getFolder();
Fun::response()
->data($folder)
->throw();
}
/**
* 读取服务器上已记录的各个邮箱的服务器地址
* @author:dc
* @time 2023/2/4 15:12
*/
public function host(){
$host = Host::_all();
Fun::response()->data($host)->throw();
}
public function lists(){
}
}