|
1
|
|
|
<?php |
|
2
|
|
|
namespace tinymeng\mailer\Connector; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* 所有Email必须继承的抽象类 |
|
6
|
|
|
*/ |
|
7
|
|
|
abstract class Gateway implements GatewayInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* 配置参数 |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $config; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* 在config中可配置的参数 |
|
17
|
|
|
* @author Tinymeng <[email protected]> |
|
18
|
|
|
* @date: 2019/9/26 15:21 |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $host; //发送email Host |
|
21
|
|
|
protected $port; //端口 25 or 456 |
|
22
|
|
|
protected $encryption; //加密 ssl |
|
23
|
|
|
protected $username; //邮箱用户名(发送人email) |
|
24
|
|
|
protected $password; //邮箱密码(如果是第三方请去设置里获取) |
|
25
|
|
|
protected $from_address;//发送人email |
|
26
|
|
|
|
|
27
|
|
|
protected $to_address; //接收人email |
|
28
|
|
|
protected $cc_address; //抄送人email |
|
29
|
|
|
protected $bcc_address; //隐性抄送人email |
|
30
|
|
|
|
|
31
|
|
|
protected $log_file = false;//记录日志 |
|
32
|
|
|
protected $host_name = "localhost"; //is used in HELO command |
|
33
|
|
|
protected $time_out = 30;//is used in fsockopen() |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 是否开启debug |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $debug = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* 是否需要登陆 |
|
43
|
|
|
* @var bool |
|
44
|
|
|
* @author Tinymeng <[email protected]> |
|
45
|
|
|
* @date: 2019/9/26 15:30 |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $auth = true; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* 附件 |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $attachments = []; |
|
54
|
|
|
/** |
|
55
|
|
|
* headers |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $headers = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Gateway constructor. |
|
62
|
|
|
* @param null $config |
|
|
|
|
|
|
63
|
|
|
* @throws \Exception |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct($config = null) |
|
66
|
|
|
{ |
|
67
|
|
|
if (!$config) { |
|
|
|
|
|
|
68
|
|
|
throw new \Exception('传入的配置不能为空'); |
|
69
|
|
|
} |
|
70
|
|
|
//默认参数 |
|
71
|
|
|
$_config = [ |
|
72
|
|
|
'host' => 'smtp.qq.com', |
|
73
|
|
|
'port' => '25', |
|
74
|
|
|
'encryption'=> false, |
|
75
|
|
|
'username'=> false, |
|
76
|
|
|
'password'=> false, |
|
77
|
|
|
'from_address'=> '', |
|
78
|
|
|
'to_address'=> '', |
|
79
|
|
|
]; |
|
80
|
|
|
$this->config = array_replace_recursive($_config,$config); |
|
81
|
|
|
if(empty($this->config['from_address'])){ |
|
82
|
|
|
$this->config['from_address'] = $this->config['username']; |
|
83
|
|
|
} |
|
84
|
|
|
if(!empty($this->config['encryption'])){ |
|
85
|
|
|
$this->config['host'] = $this->config['encryption'] . '://' . $this->config['host']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
foreach ($this->config as $key => $value) { |
|
89
|
|
|
if (property_exists($this,$key)) { |
|
90
|
|
|
$this->$key = $value; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
$this->setPassword($this->config['password']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param $password |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setPassword($password) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->password = $password; |
|
103
|
|
|
$this->setAuth(!($this->password === false)); |
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $auth |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setAuth($auth) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->auth = $auth; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Function Name: 开启debug |
|
118
|
|
|
* @param boolean $debug |
|
119
|
|
|
* @author Tinymeng <[email protected]> |
|
120
|
|
|
* @date: 2019/9/26 10:44 |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setDebug($debug){ |
|
123
|
|
|
$this->debug = $debug; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Function Name: 发送给to email |
|
128
|
|
|
* @param string $email 多个用,分割 |
|
129
|
|
|
* @return $this |
|
130
|
|
|
* @author Tinymeng <[email protected]> |
|
131
|
|
|
* @date: 2019/9/26 15:20 |
|
132
|
|
|
*/ |
|
133
|
|
|
public function toEmail($email){ |
|
134
|
|
|
$this->to_address = $email; |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Function Name: 抄送 email |
|
140
|
|
|
* @param string $email 多个用,分割 |
|
141
|
|
|
* @return $this |
|
142
|
|
|
* @author Tinymeng <[email protected]> |
|
143
|
|
|
* @date: 2019/9/26 15:20 |
|
144
|
|
|
*/ |
|
145
|
|
|
public function ccEmail($email){ |
|
146
|
|
|
$this->cc_address = $email; |
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Function Name: 隐性抄送 email |
|
152
|
|
|
* @param string $email 多个用,分割 |
|
153
|
|
|
* @return $this |
|
154
|
|
|
* @author Tinymeng <[email protected]> |
|
155
|
|
|
* @date: 2019/9/26 15:20 |
|
156
|
|
|
*/ |
|
157
|
|
|
public function bccEmail($email){ |
|
158
|
|
|
$this->bcc_address = $email; |
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* 添加附件 |
|
164
|
|
|
* @param array|string $attachments |
|
165
|
|
|
* @return $this |
|
166
|
|
|
*/ |
|
167
|
|
|
public function addAttachments($attachments){ |
|
168
|
|
|
$attachments = is_array($attachments) ? $attachments : [$attachments]; |
|
169
|
|
|
foreach ($attachments as $fileName=>$attachment){ |
|
170
|
|
|
if(file_exists($attachment) || $this->isURL($attachment)) { |
|
171
|
|
|
if(is_numeric($fileName)){ |
|
172
|
|
|
$fileName = basename($attachment); |
|
173
|
|
|
}else{ |
|
174
|
|
|
$extension = pathinfo($attachment, PATHINFO_EXTENSION); |
|
175
|
|
|
if (strpos($fileName, '.'.$extension) === false) { |
|
|
|
|
|
|
176
|
|
|
$fileName .= '.'.$extension; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
$this->attachments[$fileName] = $attachment; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param $url |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
|
|
public function isURL($url) { |
|
190
|
|
|
$parsedUrl = parse_url($url); |
|
191
|
|
|
return isset($parsedUrl['scheme']) && isset($parsedUrl['host']); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* 添加Header |
|
196
|
|
|
* @param array|string $headers |
|
197
|
|
|
* @return $this |
|
198
|
|
|
*/ |
|
199
|
|
|
public function addHeaders($headers){ |
|
200
|
|
|
if(is_array($headers)){ |
|
201
|
|
|
$this->headers = array_merge($this->headers,$headers); |
|
202
|
|
|
}else{ |
|
203
|
|
|
$this->headers[] = $headers; |
|
204
|
|
|
} |
|
205
|
|
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|