Skip to content

Commit 0306e86

Browse files
committed
feat: add inline attachment support to EmailEndpoint
Introduced optional `contentId` for inline email attachments, enabling embedding images or files directly in HTML emails. Updated tests to validate the new functionality, ensuring correct handling of `contentId`. Enhanced the README with examples demonstrating usage of inline attachments.
1 parent 7822c7e commit 0306e86

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,26 @@ $lettermint->email
5656
->text('Hello!')
5757
->headers(['X-Custom-Header' => 'Value'])
5858
->attach('document.pdf', base64_encode($fileContent))
59+
->attach('logo.png', base64_encode($logoContent), 'logo@example.com')
5960
->route('my-route-id')
6061
->idempotencyKey('unique-request-id-123')
6162
->send();
6263
```
6364

65+
#### Inline Attachments
66+
67+
You can embed images and other content in your HTML emails using content IDs:
68+
69+
```php
70+
$lettermint->email
71+
->from('sender@example.com')
72+
->to('recipient@example.com')
73+
->subject('Email with inline image')
74+
->html('<p>Here is an image: <img src="cid:logo@example.com"></p>')
75+
->attach('logo.png', base64_encode($imageContent), 'logo@example.com')
76+
->send();
77+
```
78+
6479
### Idempotency
6580

6681
To ensure that duplicate requests are not processed, you can use an idempotency key:

src/Endpoints/EmailEndpoint.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,21 @@ public function replyTo(string ...$emails): self
134134
*
135135
* @param string $filename The attachment filename.
136136
* @param string $base64Content The base64-encoded file content.
137+
* @param string|null $contentId Optional content ID for inline attachments.
137138
*/
138-
public function attach(string $filename, string $base64Content): self
139+
public function attach(string $filename, string $base64Content, ?string $contentId = null): self
139140
{
140-
$this->payload['attachments'][] = [
141+
$attachment = [
141142
'filename' => $filename,
142143
'content' => $base64Content,
143144
];
144145

146+
if ($contentId !== null) {
147+
$attachment['content_id'] = $contentId;
148+
}
149+
150+
$this->payload['attachments'][] = $attachment;
151+
145152
return $this;
146153
}
147154

tests/Endpoints/EmailEndpointTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,34 @@
157157
->send();
158158
});
159159

160+
test('it handles attachments with content_id', function () {
161+
$attachment = [
162+
'filename' => 'logo.png',
163+
'content' => 'base64encodedimage',
164+
'content_id' => 'logo@example.com',
165+
];
166+
167+
$this->httpClient
168+
->shouldReceive('post')
169+
->once()
170+
->with('/v1/send', [
171+
'from' => 'sender@example.com',
172+
'to' => ['recipient@example.com'],
173+
'subject' => 'Test Subject',
174+
'html' => '<img src="cid:logo@example.com">',
175+
'attachments' => [$attachment],
176+
], [])
177+
->andReturn(['message_id' => '123', 'status' => 'pending']);
178+
179+
$this->endpoint
180+
->from('sender@example.com')
181+
->to('recipient@example.com')
182+
->subject('Test Subject')
183+
->html('<img src="cid:logo@example.com">')
184+
->attach('logo.png', 'base64encodedimage', 'logo@example.com')
185+
->send();
186+
});
187+
160188
test('it handles custom headers', function () {
161189
$this->httpClient
162190
->shouldReceive('post')

0 commit comments

Comments
 (0)