-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailEndpoint.php
More file actions
203 lines (171 loc) · 4.43 KB
/
EmailEndpoint.php
File metadata and controls
203 lines (171 loc) · 4.43 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
<?php
namespace Lettermint\Endpoints;
class EmailEndpoint extends Endpoint
{
/**
* @var array The email payload to be sent.
*/
private array $payload = [];
/**
* @var string|null The idempotency key for the request.
*/
private ?string $idempotencyKey = null;
/**
* Set the custom headers.
*
* @example ["Key" => "Value"]
*
* @param array<string, string> $headers The custom headers.
*/
public function headers(array $headers): self
{
$this->payload['headers'] = $headers;
return $this;
}
/**
* Set the sender email address.
*
* Supports RFC 5322 addresses, e.g. <EMAIL>, <NAME> <<EMAIL>>.
*
* @example John Doe <john@acme.com>
* @example john@acme.com
*
* @param string $email The sender's email address.
*/
public function from(string $email): self
{
$this->payload['from'] = $email;
return $this;
}
/**
* Set one or more recipient email addresses.
*
* @param string ...$emails One or more recipient email addresses.
*/
public function to(string ...$emails): self
{
$this->payload['to'] = $emails;
return $this;
}
/**
* Set the subject of the email.
*
* @param string $subject The subject line.
*/
public function subject(string $subject): self
{
$this->payload['subject'] = $subject;
return $this;
}
/**
* Set the HTML body of the email.
*
* @param string|null $html The HTML content for the email body.
*/
public function html(?string $html): self
{
$this->payload['html'] = $html;
return $this;
}
/**
* Set the plain text body of the email.
*
* @param string|null $text The plain text content for the email body.
*/
public function text(?string $text): self
{
$this->payload['text'] = $text;
return $this;
}
/**
* Set one or more CC email addresses.
*
* @param string ...$emails Email addresses to be CC'd.
*/
public function cc(string ...$emails): self
{
$this->payload['cc'] = $emails;
return $this;
}
/**
* Set one or more BCC email addresses.
*
* @param string ...$emails Email addresses to be BCC'd.
*/
public function bcc(string ...$emails): self
{
$this->payload['bcc'] = $emails;
return $this;
}
/**
* Set one or more Reply-To email addresses.
*
* @param string ...$emails Reply-To email addresses.
*/
public function replyTo(string ...$emails): self
{
$this->payload['reply_to'] = $emails;
return $this;
}
/**
* Attach a file to the email.
*
* @param string $filename The attachment filename.
* @param string $base64Content The base64-encoded file content.
*/
public function attach(string $filename, string $base64Content): self
{
$this->payload['attachments'][] = [
'filename' => $filename,
'content' => $base64Content,
];
return $this;
}
/**
* Set the route id for the email.
*
* @param string $route The route id to use for sending.
*/
public function route(string $route): self
{
$this->payload['route'] = $route;
return $this;
}
/**
* Set the idempotency key for the request.
*
* @param string $key The idempotency key to ensure request uniqueness.
*/
public function idempotencyKey(string $key): self
{
$this->idempotencyKey = $key;
return $this;
}
/**
* Set custom metadata.
*
* @example ["key" => "value"]
*
* @param array<string, string> $metadata The metadata object.
*/
public function metadata(array $metadata): self
{
$this->payload['metadata'] = $metadata;
return $this;
}
/**
* Send the composed email using the current payload.
*
* @return array The API response as an associative array.
*
* @throws \Exception On HTTP or API failure.
*/
public function send(): array
{
$headers = [];
if ($this->idempotencyKey !== null) {
$headers['Idempotency-Key'] = $this->idempotencyKey;
}
return $this->httpClient->post('/v1/send', $this->payload, $headers);
}
}