|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpEmail; |
|
6
|
|
|
|
|
7
|
|
|
class EmailBuilder |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array|Address[] |
|
11
|
|
|
*/ |
|
12
|
|
|
private $toRecipients = []; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array|Address[] |
|
16
|
|
|
*/ |
|
17
|
|
|
private $ccRecipients = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array|Address[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private $bccRecipients = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Address |
|
26
|
|
|
*/ |
|
27
|
|
|
private $from; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array|Address[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private $replyTos = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $subject; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Content |
|
41
|
|
|
*/ |
|
42
|
|
|
private $content; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array|Attachment[] |
|
46
|
|
|
*/ |
|
47
|
|
|
private $attachments = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var Attachment[] |
|
51
|
|
|
*/ |
|
52
|
|
|
private $embedded = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var Header[] |
|
56
|
|
|
*/ |
|
57
|
|
|
private $headers = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return EmailBuilder |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public static function email(): self |
|
63
|
|
|
{ |
|
64
|
1 |
|
return new static(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $email |
|
69
|
|
|
* @param string|null $name |
|
70
|
|
|
* |
|
71
|
|
|
* @return EmailBuilder |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function to(string $email, ?string $name = null): self |
|
74
|
|
|
{ |
|
75
|
1 |
|
$this->toRecipients[] = new Address($email, $name); |
|
76
|
|
|
|
|
77
|
1 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $email |
|
82
|
|
|
* @param string|null $name |
|
83
|
|
|
* |
|
84
|
|
|
* @return EmailBuilder |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function cc(string $email, ?string $name = null): self |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->ccRecipients[] = new Address($email, $name); |
|
89
|
|
|
|
|
90
|
1 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $email |
|
95
|
|
|
* @param string|null $name |
|
96
|
|
|
* |
|
97
|
|
|
* @return EmailBuilder |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function bcc(string $email, ?string $name = null): self |
|
100
|
|
|
{ |
|
101
|
1 |
|
$this->bccRecipients[] = new Address($email, $name); |
|
102
|
|
|
|
|
103
|
1 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $email |
|
108
|
|
|
* @param string|null $name |
|
109
|
|
|
* |
|
110
|
|
|
* @return EmailBuilder |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function from(string $email, ?string $name = null): self |
|
113
|
|
|
{ |
|
114
|
1 |
|
$this->from = new Address($email, $name); |
|
115
|
|
|
|
|
116
|
1 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $email |
|
121
|
|
|
* @param string|null $name |
|
122
|
|
|
* |
|
123
|
|
|
* @return EmailBuilder |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function replyTo(string $email, ?string $name = null): self |
|
126
|
|
|
{ |
|
127
|
1 |
|
$this->replyTos[] = new Address($email, $name); |
|
128
|
|
|
|
|
129
|
1 |
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $subject |
|
134
|
|
|
* |
|
135
|
|
|
* @return EmailBuilder |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function withSubject(string $subject): self |
|
138
|
|
|
{ |
|
139
|
1 |
|
$this->subject = $subject; |
|
140
|
|
|
|
|
141
|
1 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param Content $content |
|
146
|
|
|
* |
|
147
|
|
|
* @return EmailBuilder |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function withContent(Content $content): self |
|
150
|
|
|
{ |
|
151
|
1 |
|
$this->content = $content; |
|
152
|
|
|
|
|
153
|
1 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @var Attachment |
|
158
|
|
|
* |
|
159
|
|
|
* @return EmailBuilder |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function attach(Attachment $attachment): self |
|
162
|
|
|
{ |
|
163
|
1 |
|
$this->attachments[] = $attachment; |
|
164
|
|
|
|
|
165
|
1 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
public function embed(Attachment $attachment, ?string $contentId = null): self |
|
169
|
|
|
{ |
|
170
|
1 |
|
$contentId = $contentId ?? $attachment->getContentId() ?? $attachment->getName(); |
|
171
|
|
|
|
|
172
|
1 |
|
$this->embedded[$contentId] = $attachment->setContentId($contentId); |
|
173
|
|
|
|
|
174
|
1 |
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param string $field |
|
179
|
|
|
* @param string $value |
|
180
|
|
|
* |
|
181
|
|
|
* @return EmailBuilder |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function addHeader(string $field, string $value): self |
|
184
|
|
|
{ |
|
185
|
1 |
|
$this->headers[] = new Header($field, $value); |
|
186
|
|
|
|
|
187
|
1 |
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return Email |
|
192
|
|
|
*/ |
|
193
|
1 |
|
public function build(): Email |
|
194
|
|
|
{ |
|
195
|
1 |
|
$email = new Email( |
|
196
|
1 |
|
$this->subject, |
|
197
|
1 |
|
$this->content, |
|
198
|
1 |
|
$this->from, |
|
199
|
1 |
|
$this->toRecipients |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
$email |
|
203
|
1 |
|
->setCcRecipients(...$this->ccRecipients) |
|
204
|
1 |
|
->setBccRecipients(...$this->bccRecipients) |
|
205
|
1 |
|
->setReplyTos(...$this->replyTos) |
|
206
|
1 |
|
->setAttachments(...$this->attachments) |
|
207
|
1 |
|
->setEmbedded(...array_values($this->embedded)) |
|
208
|
1 |
|
->setHeaders(...$this->headers); |
|
209
|
|
|
|
|
210
|
1 |
|
return $email; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|