|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Zortje\Emailer; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class Email |
|
8
|
|
|
* |
|
9
|
|
|
* @package Zortje\Emailer |
|
10
|
|
|
*/ |
|
11
|
|
|
class Email |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $from = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $to = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $subject; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $headers = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $text = ''; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $html = ''; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $tag; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set 'from' with name and email |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name Name |
|
53
|
|
|
* @param string $email Email address |
|
54
|
|
|
*/ |
|
55
|
3 |
|
public function setFrom(string $name, string $email) |
|
56
|
|
|
{ |
|
57
|
3 |
|
if (empty($name)) { |
|
58
|
1 |
|
throw new \InvalidArgumentException('From name must be provided'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { |
|
62
|
1 |
|
throw new \InvalidArgumentException("Email '$email' is invalid"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
$this->from = [ |
|
66
|
1 |
|
'name' => $name, |
|
67
|
1 |
|
'email' => $email |
|
68
|
|
|
]; |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get 'from' array with name and email |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function getFrom(): array |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->from; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set 'to' with name and email for 'to', 'cc' or 'bcc' |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $type To type: 'to', 'cc' or 'bcc' |
|
85
|
|
|
* @param string $name Name |
|
86
|
|
|
* @param string $email Email address |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function setTo(string $type, string $name, string $email) |
|
89
|
|
|
{ |
|
90
|
4 |
|
if ($type !== 'to' && $type !== 'cc' && $type !== 'bcc') { |
|
91
|
1 |
|
throw new \InvalidArgumentException("Type '$type' is not allowed, must be either 'to', 'cc' or 'bcc'"); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
if (empty($name)) { |
|
95
|
1 |
|
throw new \InvalidArgumentException('To name must be provided'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { |
|
99
|
1 |
|
throw new \InvalidArgumentException("Email '$email' is invalid"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
$this->to[] = [ |
|
103
|
1 |
|
'type' => $type, |
|
104
|
1 |
|
'name' => $name, |
|
105
|
1 |
|
'email' => $email |
|
106
|
|
|
]; |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get 'to' array with type, name and email |
|
111
|
|
|
* |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function getTo(): array |
|
115
|
|
|
{ |
|
116
|
1 |
|
return $this->to; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Set message subject |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $subject Message subject |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function setSubject(string $subject) |
|
125
|
|
|
{ |
|
126
|
1 |
|
$this->subject = $subject; |
|
127
|
1 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get message subject |
|
131
|
|
|
* |
|
132
|
|
|
* @return string Message subject |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function getSubject(): string |
|
135
|
|
|
{ |
|
136
|
1 |
|
return $this->subject; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set message header |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $header Header name |
|
143
|
|
|
* @param string $value Header value |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setHeader(string $header, string $value) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->headers[$header] = $value; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get message headers |
|
152
|
|
|
* |
|
153
|
|
|
* @return array Message headers |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getHeaders(): array |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->headers; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Set message body HTML version |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $html Message body HTML version |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function setHtml(string $html) |
|
166
|
|
|
{ |
|
167
|
1 |
|
$this->html = $html; |
|
168
|
1 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Get message body HTML version |
|
172
|
|
|
* |
|
173
|
|
|
* @return string Message body HTML version |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function getHtml(): string |
|
176
|
|
|
{ |
|
177
|
1 |
|
return $this->html; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Set message body text version |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $text Message body text version |
|
184
|
|
|
*/ |
|
185
|
1 |
|
public function setText(string $text) |
|
186
|
|
|
{ |
|
187
|
1 |
|
$this->text = $text; |
|
188
|
1 |
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Get message body text version |
|
192
|
|
|
* |
|
193
|
|
|
* @return string Message body text version |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public function getText(): string |
|
196
|
|
|
{ |
|
197
|
1 |
|
return $this->text; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Set message tag |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $tag Message tag |
|
204
|
|
|
*/ |
|
205
|
1 |
|
public function setTag($tag) |
|
206
|
|
|
{ |
|
207
|
1 |
|
$this->tag = $tag; |
|
208
|
1 |
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Get message tag |
|
212
|
|
|
* |
|
213
|
|
|
* @return string Message tag |
|
214
|
|
|
*/ |
|
215
|
1 |
|
public function getTag() |
|
216
|
|
|
{ |
|
217
|
1 |
|
return $this->tag; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|