Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,26 @@ $lettermint->email
->text('Hello!')
->headers(['X-Custom-Header' => 'Value'])
->attach('document.pdf', base64_encode($fileContent))
->attach('logo.png', base64_encode($logoContent), 'logo@example.com')
->route('my-route-id')
->idempotencyKey('unique-request-id-123')
->send();
```

#### Inline Attachments

You can embed images and other content in your HTML emails using content IDs:

```php
$lettermint->email
->from('sender@example.com')
->to('recipient@example.com')
->subject('Email with inline image')
->html('<p>Here is an image: <img src="cid:logo@example.com"></p>')
->attach('logo.png', base64_encode($imageContent), 'logo@example.com')
->send();
```

### Idempotency

To ensure that duplicate requests are not processed, you can use an idempotency key:
Expand Down
11 changes: 9 additions & 2 deletions src/Endpoints/EmailEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,21 @@ public function replyTo(string ...$emails): self
*
* @param string $filename The attachment filename.
* @param string $base64Content The base64-encoded file content.
* @param string|null $contentId Optional content ID for inline attachments.
*/
public function attach(string $filename, string $base64Content): self
public function attach(string $filename, string $base64Content, ?string $contentId = null): self
{
$this->payload['attachments'][] = [
$attachment = [
'filename' => $filename,
'content' => $base64Content,
];

if ($contentId !== null) {
$attachment['content_id'] = $contentId;
}

$this->payload['attachments'][] = $attachment;

return $this;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/Endpoints/EmailEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,34 @@
->send();
});

test('it handles attachments with content_id', function () {
$attachment = [
'filename' => 'logo.png',
'content' => 'base64encodedimage',
'content_id' => 'logo@example.com',
];

$this->httpClient
->shouldReceive('post')
->once()
->with('/v1/send', [
'from' => 'sender@example.com',
'to' => ['recipient@example.com'],
'subject' => 'Test Subject',
'html' => '<img src="cid:logo@example.com">',
'attachments' => [$attachment],
], [])
->andReturn(['message_id' => '123', 'status' => 'pending']);

$this->endpoint
->from('sender@example.com')
->to('recipient@example.com')
->subject('Test Subject')
->html('<img src="cid:logo@example.com">')
->attach('logo.png', 'base64encodedimage', 'logo@example.com')
->send();
});

test('it handles custom headers', function () {
$this->httpClient
->shouldReceive('post')
Expand Down