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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const response = await lettermint.email
'X-Custom-Header': 'Custom Value',
})
.attach('attachment.txt', Buffer.from('Hello World').toString('base64'))
.attach('logo.png', Buffer.from('...').toString('base64'), 'logo') // Inline attachment
.idempotencyKey('unique-id-123')
.metadata({
foo: 'bar',
Expand Down Expand Up @@ -90,7 +91,7 @@ Methods for sending emails:
- `bcc(...emails: string[])`: Set one or more BCC email addresses
- `replyTo(...emails: string[])`: Set one or more Reply-To email addresses
- `headers(headers: Record<string, string>)`: Set custom headers for the email
- `attach(filename: string, base64Content: string)`: Attach a file to the email
- `attach(filename: string, base64Content: string, content_id?: string)`: Attach a file to the email. Optional `content_id` for inline attachments.
- `route(route: string)`: Set the routing key for the email
- `idempotencyKey(key: string)`: Set an idempotency key to prevent duplicate email sends
- `metadata(metadata: Record<string, string>)`: Set metadata for the email
Expand Down
22 changes: 22 additions & 0 deletions src/endpoints/email.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,28 @@ describe('EmailEndpoint', () => {
});
});

it('should add attachments with content_id', () => {
const result = emailEndpoint.attach('image.png', 'base64imagedata', 'logo');

expect(result).toBe(emailEndpoint);

return emailEndpoint.send().then(() => {
expect(client.post).toHaveBeenCalledWith(
'/send',
expect.objectContaining({
attachments: [
{
filename: 'image.png',
content: 'base64imagedata',
content_id: 'logo',
},
],
}),
undefined
);
});
});

it('should set the route', () => {
const result = emailEndpoint.route('test-route');

Expand Down
9 changes: 8 additions & 1 deletion src/endpoints/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export interface EmailAttachment {
* The base64-encoded content of the attachment
*/
content: string;

/**
* The Content-ID for inline attachments (optional)
*/
content_id?: string;
}

/**
Expand Down Expand Up @@ -279,16 +284,18 @@ export class EmailEndpoint extends Endpoint {
*
* @param filename The attachment filename
* @param content The base64-encoded file content
* @param content_id The Content-ID for inline attachments (optional)
* @returns The current instance for chaining
*/
public attach(filename: string, content: string): this {
public attach(filename: string, content: string, content_id?: string): this {
if (!this.payload.attachments) {
this.payload.attachments = [];
}

this.payload.attachments.push({
filename,
content,
...(content_id && { content_id }),
});

return this;
Expand Down