MailFun.php
10.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
namespace Lib\Mail;
use Lib\UploadFile;
use Model\bodySql;
use Model\folderSql;
use Model\listsSql;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
/**
* 函数
* @time 2022/8/1 16:02
* Class MailFun
* @package App\Mail\lib
*/
class MailFun {
/**
* json encode
* @param $data
* @param int $option
* @return false|string
* @time 2022/8/2 15:57
*/
public static function json_en($data,$option=\JSON_UNESCAPED_UNICODE){
return \json_encode($data,$option);
}
/**
* @param $str
* @param string $encode
* @return string
* @author:dc
* @time 2023/3/27 13:55
*/
public static function mb_coding(string $str,array $appcode=[]){
try {
$encode = @mb_detect_encoding($str, array_merge(array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'),$appcode));
if($encode){
return @mb_convert_encoding($str, 'UTF-8', $encode);
}
}catch (\Throwable $e){
}
return $str;
}
/**
* 验证是否有附件 BODYSTRUCTURE值
* @param $BODYSTRUCTURE
* @return int
* @author:dc
* @time 2022/11/1 10:57
*/
public static function isFile($BODYSTRUCTURE):int {
// foreach ($BODYSTRUCTURE as $item){
// if($item[0] === 'APPLICATION'){
// return 1;
// }
// }
// return 0;
$json = is_array($BODYSTRUCTURE) ? implode(' ',$BODYSTRUCTURE) : $BODYSTRUCTURE;
return str_contains(mb_strtolower($json), '"attachment"');
}
/**
* 验证body里面是否有附件
* @param array $body
* @return bool
* @author:dc
* @time 2023/6/25 15:03
*/
public static function isBodyFile(array $body){
foreach ($body as $item){
// if(v.name && v.filename && v.signName && !v['content-id']){
if(!empty($item['name']) && !empty($item['filename']) && !empty($item['signName']) && empty($item['content-id'])){
return 1;
}
}
return 0;
}
/**
* 匹配邮箱
* @param $str
* @return mixed|string
* @author:dc
* @time 2023/11/24 10:04
*/
public static function pregEmail($str){
preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/',$str,$email);
if(empty($email[0])){
// 邮箱2
preg_match('/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/',$str,$email);
}
return $email[0]??'';
}
/**
* 邮件收件人/发件人
* @param $str
* @return array
* @author:dc
* @time 2022/11/8 9:36
*/
public static function toOrFrom($str){
$strs = explode(',',$str);
foreach ($strs as $k=>$s){
$email = self::pregEmail($s);
if(empty($email)){
$s = [
'email' => '',
'name' => $s
];
}else{
$s = str_replace([$email,'"','<','>','>','<'],'',$s);
$s = trim($s);
$s = [
'email' => $email,
'name' => $s
];
}
if(empty($s['name'])){
$s['name'] = explode('@',$s['email'])[0]??'';
}
if(!empty($s['email'])){
$strs[$k] = $s;
}else{
unset($strs[$k]);
}
}
return array_values($strs);
}
/**
* 发送邮件
* @param array $data 数据
* @param array $email 邮箱信息
* @return array
* @throws \Lib\Err
* @throws \PHPMailer\PHPMailer\Exception
* @author:dc
* @time 2023/4/11 9:12
*/
public static function sendEmail(array $data, array $email){
$smtp = self::getHostPort($email['smtp'],465);
// 邮件对象
$mail = new PHPMailer();
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = $smtp['host']; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = $email['email']; //SMTP username
$mail->Password = base64_decode($email['password']); //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = $smtp['port']; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->CharSet = 'utf-8';
$mail->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE;
// 是否验证证书
// if(in_array(parse_url($smtp['host'])['host']??'1-1',['mail.laser-tech.com.cn'])){
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => false, // 有的证书和域名不匹配,这里关闭认证
'verify_peer_name' => false,// 有的证书和域名不匹配,这里关闭认证
]
];
// }
//Recipients,设置发件人
$mail->setFrom($email['email'], $data['nickname']??'');// 显示邮件来自谁
// //设置收件人
foreach ($data['tos'] as $to){
$mail->addAddress($to['email'], $to['name']??'');
}
//回复到那个邮件
if(!empty($data['reply_to'])){
if(is_string($data['reply_to'])){
$mail->addReplyTo($data['reply_to']);
}else{
$mail->addReplyTo($data['reply_to']['email'], $data['reply_to']['name']); //Add a recipient
}
}
// 抄送
if(!empty($data['cc'])){
foreach ($data['cc'] as $to){
$mail->addCC($to['email'], $to['name']??'');
}
}
// 密送
if(!empty($data['bcc'])){
foreach ($data['bcc'] as $to){
$mail->addBCC($to['email'], $to['name']??'');
}
}
//Attachments 附件
if(!empty($data['attachment'])){
foreach ($data['attachment'] as $file){
$mail->addAttachment($file['path'], $file['filename']);
}
}
// 回执,阅读后收回执的邮箱
if(!empty($data['receipt'])){
$mail->ConfirmReadingTo = $email['email'];
}
// 是否紧急邮件
// Options: null (default), 1 = High, 3 = Normal, 5 = low.
$mail->Priority = $data['priority']??3;
//Content 主题,标题
$mail->Subject = $data['subject'];
$mail->isHTML(true); //Set email format to HTML
$mail->Body = $data['body'];// html格式的内容
// 发送
if($mail->send()){
return [true,$mail->getLastMessageID()];
}
return [false,$mail->ErrorInfo];
}
/**
* 文件夹名 编译
* @param $folder
* @return string
* @author:dc
* @time 2023/3/14 9:37
*/
public static function folderEncoding($folder):string {
return mb_convert_encoding($folder,'UTF7-IMAP','UTF-8');
}
/**
* 是否是base64
* @param $str
* @return bool
* @author:dc
* @time 2023/4/3 10:24
*/
public static function isBase64($str){
$oldStr = str_replace(["\n","\r"],'',rtrim($str,'='));
return $oldStr == rtrim(base64_encode(base64_decode($str)),'=');
}
/**
* 测试登录
* @param $smtp
* @param $email
* @param $password
* @return bool
* @author:dc
* @time 2023/11/7 10:47
*/
public static function smtpLoginTest($smtp,$email,$password){
$smtp = self::getHostPort($smtp,465);
// 邮件对象
$mail = new PHPMailer();
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = $smtp['host']; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = $email; //SMTP username
$mail->Password = $password; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = $smtp['port']; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->CharSet = 'utf-8';
$mail->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE;
// 是否验证证书
// if(in_array(parse_url($smtp['host'])['host']??'1-1',['mail.laser-tech.com.cn'])){
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => false, // 有的证书和域名不匹配,这里关闭认证
'verify_peer_name' => false,// 有的证书和域名不匹配,这里关闭认证
]
];
// }
try {
return $mail->smtpConnect();
}catch (\Throwable $e){}
return false;
}
/**
* @param $host
* @param int $port
* @return array
* @author:dc
* @time 2024/3/6 9:21
*/
public static function getHostPort($host,$port=0,$ssl = 'ssl://'){
$host = ltrim($host,'/');
$arr = explode(":",$host);
if(str_contains($host, '//')){
return [
'host' => $arr[0].':'.$arr[1],
'port' => empty($arr[2]) ? $port : $arr[2],
];
}else{
return [
'host' => $ssl.$arr[0],
'port' => empty($arr[1]) ? $port : $arr[1],
];
}
}
}