Skip to content

Commit 6ca8411

Browse files
committed
Add support for Email Logs API
1 parent ef3cf27 commit 6ca8411

File tree

12 files changed

+742
-33
lines changed

12 files changed

+742
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## [Unreleased]
22

33
- Add StatsApi with get, byDomain, byCategory, byEmailServiceProvider, byDate endpoints
4+
- Add Email Logs API
45

56
## [4.4.0] - 2025-12-08
67

README.md

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -227,65 +227,43 @@ See transport usage below:
227227
Email API:
228228

229229
- Send an email (Transactional stream) – [`sending/minimal.ts`](examples/sending/minimal.ts)
230-
231230
- Send an email (Bulk stream) – [`bulk/send-mail.ts`](examples/bulk/send-mail.ts)
232-
233231
- Send an email with a Template (Transactional) – [`sending/template.ts`](examples/sending/template.ts)
234-
235232
- Send an email with a Template (Bulk) – [`bulk/send-mail.ts`](examples/bulk/send-mail.ts)
236-
237233
- Batch send (Transactional) – [`batch/transactional.ts`](examples/batch/transactional.ts)
238-
239-
- Batch send (Bulk) – [`batch/bulk.ts`](examples/batch/bulk.ts)
240-
234+
- Batch send (Bulk) – [`batch/bulk.ts`](examples/bulk/bulk.ts)
241235
- Batch send with Template (Transactional) – [`batch/template.ts`](examples/batch/template.ts)
242-
243236
- Batch send with Template (Bulk) – [`batch/template.ts`](examples/batch/template.ts)
244-
245237
- Sending domain management CRUD – [`sending-domains/everything.ts`](examples/sending-domains/everything.ts)
238+
- Sending stats (aggregated and by domain, category, ESP, date) – [`stats/everything.ts`](examples/stats/everything.ts)
239+
- Email logs (list with filters, get by message ID) – [`email-logs/everything.ts`](examples/email-logs/everything.ts)
246240

247241
Email Sandbox (Testing):
248242

249243
- Send an email (Sandbox) – [`testing/send-mail.ts`](examples/testing/send-mail.ts)
250-
251244
- Send an email with a Template (Sandbox) – [`testing/template.ts`](examples/testing/template.ts)
252-
253245
- Batch send (Sandbox) – [`batch/sandbox.ts`](examples/batch/sandbox.ts)
254-
255246
- Batch send with Template (Sandbox) – [`batch/sandbox.ts`](examples/batch/sandbox.ts)
256-
257247
- Message management CRUD – [`testing/messages.ts`](examples/testing/messages.ts)
258-
259248
- Inbox management CRUD – [`testing/inboxes.ts`](examples/testing/inboxes.ts)
260-
261249
- Project management CRUD – [`testing/projects.ts`](examples/testing/projects.ts)
262-
263250
- Attachments operations – [`testing/attachments.ts`](examples/testing/attachments.ts)
264251

265252
Contact management:
266253

267254
- Contacts CRUD & listing – [`contacts/everything.ts`](examples/contacts/everything.ts)
268-
269255
- Contact lists CRUD – [`contact-lists/everything.ts`](examples/contact-lists/everything.ts)
270-
271256
- Custom fields CRUD – [`contact-fields/everything.ts`](examples/contact-fields/everything.ts)
272-
273257
- Import/Export – [`contact-imports/everything.ts`](examples/contact-imports/everything.ts), [`contact-exports/everything.ts`](examples/contact-exports/everything.ts)
274-
275258
- Events – [`contact-events/everything.ts`](examples/contact-events/everything.ts)
276259

277260
General API:
278261

279262
- Templates CRUD – [`templates/everything.ts`](examples/templates/everything.ts)
280-
281263
- Suppressions (find & delete) – [`sending/suppressions.ts`](examples/sending/suppressions.ts)
282-
283264
- Billing info – [`general/billing.ts`](examples/general/billing.ts)
284-
285265
- Accounts info – [`general/accounts.ts`](examples/general/accounts.ts)
286-
287266
- Permissions listing – [`general/permissions.ts`](examples/general/permissions.ts)
288-
289267
- Users listing – [`general/account-accesses.ts`](examples/general/account-accesses.ts)
290268

291269
## Contributing

examples/email-logs/everything.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { MailtrapClient } from "mailtrap";
2+
3+
const TOKEN = "<YOUR-TOKEN-HERE>";
4+
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>";
5+
6+
const client = new MailtrapClient({
7+
token: TOKEN,
8+
accountId: Number(ACCOUNT_ID),
9+
});
10+
11+
async function emailLogsFlow() {
12+
try {
13+
// List email logs (paginated)
14+
const list = await client.emailLogs.getList();
15+
console.log("Email logs:", list.messages.length, "messages, total:", list.total_count);
16+
if (list.messages.length > 0) {
17+
console.log("First message:", list.messages[0].message_id, list.messages[0].subject);
18+
}
19+
20+
// List with filters (date range, category, status). Filter values can be single or array.
21+
const now = new Date();
22+
const twoDaysAgo = new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000);
23+
const filtered = await client.emailLogs.getList({
24+
filters: {
25+
sent_after: twoDaysAgo.toISOString(),
26+
sent_before: now.toISOString(),
27+
subject: { operator: "not_empty" },
28+
to: { operator: "ci_equal", value: "recipient@example.com" },
29+
category: { operator: "equal", value: ["Welcome Email", "Forget Password"] },
30+
},
31+
});
32+
console.log("Filtered logs:", filtered.messages.length);
33+
34+
// Next page (use search_after from previous response next_page_cursor)
35+
if (list.next_page_cursor) {
36+
const nextPage = await client.emailLogs.getList({
37+
search_after: list.next_page_cursor,
38+
});
39+
console.log("Next page:", nextPage.messages.length, "messages");
40+
}
41+
42+
// Get a single message by ID
43+
if (list.messages.length > 0) {
44+
const messageId = list.messages[0].message_id;
45+
const message = await client.emailLogs.get(messageId);
46+
console.log("Single message:", message.subject, "events:", message.events?.length ?? 0);
47+
}
48+
} catch (error) {
49+
console.error("Error in emailLogsFlow:", error instanceof Error ? error.message : String(error));
50+
}
51+
}
52+
53+
emailLogsFlow();

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
"version": "4.4.0",
55
"author": "Railsware Products Studio LLC",
66
"dependencies": {
7-
"axios": ">=0.27"
7+
"axios": ">=0.27",
8+
"qs": "^6.15.0"
89
},
910
"devDependencies": {
10-
"rimraf": "^5.0.5",
1111
"@babel/core": "^7.20.5",
1212
"@babel/preset-env": "^7.20.2",
1313
"@babel/preset-typescript": "^7.18.6",
1414
"@jest/globals": "^29.3.1",
1515
"@types/jest": "^29.5.3",
1616
"@types/node": "^18.15.11",
1717
"@types/nodemailer": "^6.4.9",
18+
"@types/qs": "^6.15.0",
1819
"@typescript-eslint/eslint-plugin": "^5.57.1",
1920
"@typescript-eslint/parser": "^5.57.1",
2021
"axios-mock-adapter": "^1.21.2",
@@ -28,6 +29,7 @@
2829
"jest": "^29.3.1",
2930
"nodemailer": "^7.0.7",
3031
"prettier": "^2.6.2",
32+
"rimraf": "^5.0.5",
3133
"ts-node": "^10.2.1",
3234
"typescript": "^5.0.3"
3335
},

0 commit comments

Comments
 (0)