MailFun.php
6.4 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
<?php
namespace Helper\Mail;
use Illuminate\Support\Facades\Storage;
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 $string
* @param string $charset
* @return string
* @time 2022/8/15 9:31
*/
public static function decodeMimeStr($string, $charset = 'utf-8') {
$newString = '';
$elements = imap_mime_header_decode($string);
// print_r($elements);
for($i = 0; $i < count($elements); $i++) {
if($elements[$i]->charset == 'default') {
$elements[$i]->charset = 'iso-8859-1';
}
$newString .= self::convertStringEncoding($elements[$i]->text, $elements[$i]->charset, $charset);
}
return $newString;
}
public static function convertStringEncoding($string, $fromEncoding, $toEncoding) {
$convertedString = null;
if($string && $fromEncoding != $toEncoding) {
$convertedString = @iconv($fromEncoding, $toEncoding . '//IGNORE', $string);
if(!$convertedString && extension_loaded('mbstring')) {
$convertedString = @mb_convert_encoding($string, $toEncoding, $fromEncoding);
}
}
return $convertedString ?: $string;
}
/**
* 验证是否有附件 BODYSTRUCTURE值
* @param array $BODYSTRUCTURE
* @return int
* @author:dc
* @time 2022/11/1 10:57
*/
public static function isFile(array $BODYSTRUCTURE):int {
// foreach ($BODYSTRUCTURE as $item){
// if($item[0] === 'APPLICATION'){
// return 1;
// }
// }
// return 0;
$json = json_encode($BODYSTRUCTURE);
return strpos($json,'"attachment"')!==false;
}
/**
* 邮件收件人/发件人
* @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){
preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/',$s,$email);
if(empty($email[0])){
$s = [
'email' => '',
'name' => $s
];
}else{
$s = str_replace([$email[0],'"','<','>','>','<'],'',$s);
$s = trim($s);
$s = [
'email' => $email[0],
'name' => $s
];
}
if(empty($s['name'])){
$s['name'] = explode('@',$s['email'])[0]??'';
}
if(!empty($s['email'])){
$strs[$k] = $s;
}else{
unset($strs[$k]);
}
}
return $strs;
}
/**
* @param string $smtp smtp服务器地址
* @param string $username 发件人
* @param string $password 发件人密码
* @param string $nickname 昵称
* @param string|array $to_email 收件人,邮件或['email'=>'','name'=>'']
* @param string $subject 标题,主题
* @param string $body 文本内容
* @param array $files 文件 ['origin_name'=>'','path'=>'']
* @param false $receipt 是否回执
* @param int $priority 是否紧急 1紧急 3正常 5慢
* @return bool
* @throws \PHPMailer\PHPMailer\Exception
* @author:dc
* @time 2022/11/11 14:26
*/
public static function sendEmail(string $smtp,string $username,string $password,string $nickname,$to_email,string $subject,string $body,$files=[],$receipt=false,$priority=3){
// 邮件对象
$mail = new PHPMailer(true);
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_CLIENT;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = $smtp; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = $username; //SMTP username
$mail->Password = $password; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //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;
//Recipients,设置发件人
$mail->setFrom($username, $nickname);// 显示邮件来自谁
// //Add a recipient,设置收件人 这里必须是一对一发送
if(is_array($to_email)){
$mail->addAddress($to_email['email'], $to_email['name']);
}else{
$mail->addAddress($to_email, '');
}
// //回复到那个邮件
// $mail->addAddress($reply_to['email'], $reply_to['name']); //Add a recipient
// // 抄送
// $mail->addCC($cc['email'],$cc['name']);//
// // 密送
// $mail->addBCC($bcc['email'],$bcc['name']);
//Attachments 附件
if($files){
foreach ($files as $file){
// 添加到邮箱中
$mail->addAttachment($file['path'], $file['origin_name']); //Add attachments
}
}
// 回执,阅读后收回执的邮箱
if($receipt){
$mail->ConfirmReadingTo = $receipt;
}
// 是否紧急邮件
// Options: null (default), 1 = High, 3 = Normal, 5 = low.
$mail->Priority = $priority;
//Content 主题,标题
$mail->Subject = $subject;
$mail->isHTML(true); //Set email format to HTML
$mail->Body = $body;// html格式的内容
// 发送
if($mail->send()){
return true;
}
throw new \Exception($mail->ErrorInfo,500);
}
}