Skip to content

Commit 523d485

Browse files
Update README.md to enhance installation and usage instructions, improve clarity on API features, and add examples for email sending and contact management functionalities.
1 parent 0fa2b59 commit 523d485

File tree

1 file changed

+209
-106
lines changed

1 file changed

+209
-106
lines changed

README.md

Lines changed: 209 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +5,192 @@
55
[![NPM](https://shields.io/npm/v/mailtrap?logo=npm&logoColor=white)](https://www.npmjs.com/package/mailtrap)
66
[![downloads](https://shields.io/npm/d18m/mailtrap)](https://www.npmjs.com/package/mailtrap)
77

8-
98
## Prerequisites
109

11-
To get the most out of this official Mailtrap.io Node.js SDK:
10+
To get the most of this official Mailtrap.io Node.js SDK:
11+
1212
- [Create a Mailtrap account](https://mailtrap.io/signup)
13-
- [Verify your domain](https://mailtrap.io/sending/domains)
1413

15-
## Supported functionality
16-
17-
This NPM package offers integration with the [official API](https://api-docs.mailtrap.io/) for [Mailtrap](https://mailtrap.io).
18-
19-
Quickly integrate Mailtrap with your Node.js app.
20-
21-
Currently, with this SDK you can:
22-
- Email API/SMTP
23-
- Send an email
24-
- Send an email with a template
25-
- Send a batch of emails
26-
- Email Sandbox (Testing)
27-
- Send an email
28-
- Send an email with a template
29-
- Message management
30-
- Inbox management
31-
- Project management
32-
- Contact management
33-
- Contact Events
34-
- Contact Exports
35-
- Contact Fields
36-
- Contact Imports
37-
- Contact Lists
38-
- Contacts
39-
- General
40-
- Templates
41-
- Suppressions management
42-
- Account access management
43-
- Permissions management
44-
- List accounts you have access to
45-
14+
- [Verify your domain](https://mailtrap.io/sending/domains)
4615

4716
## Installation
4817

49-
Use yarn or npm to install the package:
18+
You can install the package via [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/):
5019

51-
```sh
52-
yarn add mailtrap
53-
54-
# or, if you are using NPM:
20+
```bash
5521
npm install mailtrap
56-
```
5722

23+
# or, if you are using Yarn:
24+
yarn add mailtrap
25+
```
5826

5927
## Usage
6028

61-
### Minimal
29+
You should use ES modules or CommonJS imports in your application to load the package.
30+
31+
### Minimal usage (Transactional sending)
32+
33+
The quickest way to send a single transactional email with only the required parameters:
6234

6335
```ts
64-
import { MailtrapClient } from "mailtrap"
36+
import { MailtrapClient } from "mailtrap";
6537

66-
/**
67-
* For this example to work, you need to set up a sending domain,
68-
* and obtain a token that is authorized to send from the domain.
69-
*/
38+
const mailtrap = new MailtrapClient({
39+
token: process.env.MAILTRAP_API_KEY, // your API key here https://mailtrap.io/api-tokens
40+
});
7041

71-
const TOKEN = "<YOUR-TOKEN-HERE>";
72-
const SENDER_EMAIL = "<[email protected]>";
73-
const RECIPIENT_EMAIL = "<[email protected]>";
42+
mailtrap
43+
.send({
44+
from: { name: "Mailtrap Test", email: "[email protected]" },
45+
to: [{ email: "[email protected]" }],
46+
subject: "Hello from Mailtrap Node.js",
47+
text: "Plain text body",
48+
})
49+
.then(console.log)
50+
.catch(console.error);
51+
```
52+
53+
### Sandbox vs Production (easy switching)
54+
55+
Mailtrap lets you test safely in the Email Sandbox and then switch to Production (Sending) with one flag.
56+
57+
Example `.env` variables (or export in shell):
7458

75-
const client = new MailtrapClient({ token: TOKEN });
59+
```
60+
MAILTRAP_API_KEY=your_api_token # https://mailtrap.io/api-tokens
61+
MAILTRAP_USE_SANDBOX=true # true/false toggle
62+
MAILTRAP_INBOX_ID=123456 # Only needed for sandbox
63+
```
7664

77-
const sender = { name: "Mailtrap Test", email: SENDER_EMAIL };
65+
Bootstrap logic:
66+
67+
```ts
68+
import { MailtrapClient } from "mailtrap";
69+
70+
const apiKey = process.env.MAILTRAP_API_KEY;
71+
const isSandbox = process.env.MAILTRAP_USE_SANDBOX === "true";
72+
const inboxId = isSandbox ? Number(process.env.MAILTRAP_INBOX_ID) : undefined; // required only for sandbox
73+
74+
const client = new MailtrapClient({
75+
token: apiKey,
76+
sandbox: isSandbox,
77+
testInboxId: inboxId, // undefined is ignored for production
78+
});
7879

7980
client
8081
.send({
81-
from: sender,
82-
to: [{ email: RECIPIENT_EMAIL }],
83-
subject: "Hello from Mailtrap!",
84-
text: "Welcome to Mailtrap Sending!",
82+
from: {
83+
name: "Mailtrap Test",
84+
email: isSandbox ? "[email protected]" : "[email protected]",
85+
},
86+
to: [{ email: "[email protected]" }],
87+
subject: isSandbox ? "[SANDBOX] Demo email" : "Welcome onboard",
88+
text: "This is a minimal body for demonstration purposes.",
89+
})
90+
.then(console.log)
91+
.catch(console.error);
92+
```
93+
94+
Bulk stream example (optional) differs only by setting `bulk: true`:
95+
96+
```ts
97+
const bulkClient = new MailtrapClient({ token: apiKey, bulk: true });
98+
```
99+
100+
Recommendations:
101+
102+
- Toggle sandbox with `MAILTRAP_USE_SANDBOX`.
103+
104+
- Use separate API tokens for Production and Sandbox.
105+
106+
- Keep initialisation in a single factory object/service so that switching is centralised.
107+
108+
### Full-featured usage example
109+
110+
```ts
111+
import { MailtrapClient } from "mailtrap";
112+
import fs from "node:fs";
113+
import path from "node:path";
114+
115+
// Init Mailtrap client depending on your needs
116+
const mailtrap = new MailtrapClient({
117+
token: process.env.MAILTRAP_API_KEY, // your API token
118+
bulk: false, // set to true for bulk email sending (false by default)
119+
sandbox: false, // set to true for sandbox mode (false by default)
120+
testInboxId: undefined, // optional, only for sandbox mode
121+
});
122+
123+
const welcomeImage = fs.readFileSync(path.join(__dirname, "welcome.png"));
124+
125+
mailtrap
126+
.send({
127+
category: "Integration Test",
128+
custom_variables: {
129+
user_id: "45982",
130+
batch_id: "PSJ-12",
131+
},
132+
from: { name: "Mailtrap Test", email: "[email protected]" },
133+
reply_to: { email: "[email protected]" },
134+
to: [{ name: "Jon", email: "[email protected]" }],
135+
cc: [{ email: "[email protected]" }, { email: "[email protected]" }],
136+
bcc: [{ email: "[email protected]" }],
137+
subject: "Best practices of building HTML emails",
138+
text: "Hey! Learn the best practices of building HTML emails and play with ready-to-go templates. Mailtrap's Guide on How to Build HTML Email is live on our blog",
139+
html: `
140+
<html>
141+
<body>
142+
<p><br>Hey</br>
143+
Learn the best practices of building HTML emails and play with ready-to-go templates.</p>
144+
<p><a href="https://mailtrap.io/blog/build-html-email/">Mailtrap's Guide on How to Build HTML Email</a> is live on our blog</p>
145+
<img src="cid:welcome.png">
146+
</body>
147+
</html>
148+
`,
149+
attachments: [
150+
{
151+
filename: "welcome.png",
152+
content_id: "welcome.png",
153+
disposition: "inline",
154+
content: welcomeImage,
155+
},
156+
],
157+
headers: {
158+
"X-Message-Source": "domain.com",
159+
"X-Mailer": "Mailtrap Node.js Client",
160+
},
161+
})
162+
.then(console.log)
163+
.catch(console.error);
164+
165+
// OR Template email sending
166+
mailtrap
167+
.send({
168+
from: { name: "Mailtrap Test", email: "[email protected]" },
169+
reply_to: { email: "[email protected]" },
170+
to: [{ name: "Jon", email: "[email protected]" }],
171+
template_uuid: "bfa432fd-0000-0000-0000-8493da283a69",
172+
template_variables: {
173+
user_name: "Jon Bush",
174+
next_step_link: "https://mailtrap.io/",
175+
get_started_link: "https://mailtrap.io/",
176+
onboarding_video_link: "some_video_link",
177+
company: {
178+
name: "Best Company",
179+
address: "Its Address",
180+
},
181+
products: [
182+
{
183+
name: "Product 1",
184+
price: 100,
185+
},
186+
{
187+
name: "Product 2",
188+
price: 200,
189+
},
190+
],
191+
isBool: true,
192+
int: 123,
193+
},
85194
})
86195
.then(console.log)
87196
.catch(console.error);
@@ -92,98 +201,92 @@ client
92201
> NOTE: [Nodemailer](https://www.npmjs.com/package/nodemailer) is needed as a dependency.
93202
94203
```sh
95-
yarn add nodemailer
96-
97-
# or, if you are using NPM:
98204
npm install nodemailer
205+
206+
# or, if you are using Yarn:
207+
yarn add nodemailer
99208
```
100209

101-
If you're using TypeScript, install `@types/nodemailer` as a `devDependency`.
210+
If you're using TypeScript, install `@types/nodemailer` as a `devDependency`:
102211

103212
```sh
104-
yarn add --dev @types/nodemailer
105-
106-
# or, if you are using NPM:
107213
npm install -D @types/nodemailer
214+
215+
# or, if you are using Yarn:
216+
yarn add --dev @types/nodemailer
108217
```
109218

110-
You can provide Mailtrap-specific keys like `category`, `customVariables`, `templateUuid`, and `templateVariables`.
219+
You can provide Mailtrap-specific keys like `category`, `custom_variables`, `template_uuid`, and `template_variables`.
111220

112221
See transport usage below:
113222

114223
- [Transport](examples/sending/transport.ts)
115224

116-
## Examples
225+
## Supported functionality & Examples
226+
227+
Email API:
228+
229+
- Send an email (Transactional stream) – [`sending/minimal.ts`](examples/sending/minimal.ts)
230+
231+
- Send an email (Bulk stream) – [`bulk/send-mail.ts`](examples/bulk/send-mail.ts)
232+
233+
- Send an email with a Template (Transactional) – [`sending/template.ts`](examples/sending/template.ts)
117234

118-
Refer to the [`examples`](examples) folder for the source code of this and other advanced examples.
235+
- Send an email with a Template (Bulk) – [`bulk/send-mail.ts`](examples/bulk/send-mail.ts)
119236

120-
### Contact Events API
237+
- Batch send (Transactional) – [`batch/transactional.ts`](examples/batch/transactional.ts)
121238

122-
- [Contact Events](examples/contact-events/everything.ts)
239+
- Batch send (Bulk) – [`batch/bulk.ts`](examples/batch/bulk.ts)
123240

124-
### Contact Exports API
241+
- Batch send with Template (Transactional) – [`batch/template.ts`](examples/batch/template.ts)
125242

126-
- [Contact Exports](examples/contact-exports/everything.ts)
243+
- Batch send with Template (Bulk) – [`batch/template.ts`](examples/batch/template.ts)
127244

128-
### Contact Fields API
245+
- Sending domain management CRUD – [`sending-domains/everything.ts`](examples/sending-domains/everything.ts)
129246

130-
- [Contact Fields](examples/contact-fields/everything.ts)
247+
Email Sandbox (Testing):
131248

132-
### Contact Imports API
249+
- Send an email (Sandbox) – [`testing/send-mail.ts`](examples/testing/send-mail.ts)
133250

134-
- [Contact Imports](examples/contact-imports/everything.ts)
251+
- Send an email with a Template (Sandbox) – [`testing/template.ts`](examples/testing/template.ts)
135252

136-
### Contact Lists API
253+
- Batch send (Sandbox) – [`batch/sandbox.ts`](examples/batch/sandbox.ts)
137254

138-
- [Contact Lists](examples/contact-lists/everything.ts)
255+
- Batch send with Template (Sandbox) – [`batch/sandbox.ts`](examples/batch/sandbox.ts)
139256

140-
### Contacts API
257+
- Message management CRUD – [`testing/messages.ts`](examples/testing/messages.ts)
141258

142-
- [Contacts](examples/contacts/everything.ts)
259+
- Inbox management CRUD – [`testing/inboxes.ts`](examples/testing/inboxes.ts)
143260

144-
### Sending API
261+
- Project management CRUD – [`testing/projects.ts`](examples/testing/projects.ts)
145262

146-
- [Advanced](examples/sending/everything.ts)
147-
- [Minimal](examples/sending/minimal.ts)
148-
- [Send email using template](examples/sending/template.ts)
149-
- [Suppressions management](examples/sending/suppressions.ts)
150-
- [Nodemailer transport](examples/sending/transport.ts)
263+
- Attachments operations – [`testing/attachments.ts`](examples/testing/attachments.ts)
151264

152-
### Batch Sending API
265+
Contact management:
153266

154-
- [Transactional emails](examples/batch/transactional.ts)
155-
- [Bulk emails](examples/batch/bulk.ts)
156-
- [Sandbox emails](examples/batch/sandbox.ts)
157-
- [Emails from template](examples/batch/template.ts)
267+
- Contacts CRUD & listing – [`contacts/everything.ts`](examples/contacts/everything.ts)
158268

159-
### Bulk Sending API
269+
- Contact lists CRUD – [`contact-lists/everything.ts`](examples/contact-lists/everything.ts)
160270

161-
- [Send mail](examples/bulk/send-mail.ts)
162-
- [Nodemailer Transport](examples/bulk/transport.ts)
271+
- Custom fields CRUD – [`contact-fields/everything.ts`](examples/contact-fields/everything.ts)
163272

164-
### Templates API
273+
- Import/Export – [`contact-imports/everything.ts`](examples/contact-imports/everything.ts), [`contact-exports/everything.ts`](examples/contact-exports/everything.ts)
165274

166-
- [Templates](examples/templates/everything.ts)
275+
- Events – [`contact-events/everything.ts`](examples/contact-events/everything.ts)
167276

168-
### Email Sandbox (Testing) API
277+
General API:
169278

170-
- [Attachments](examples/testing/attachments.ts)
171-
- [Inboxes](examples/testing/inboxes.ts)
172-
- [Messages](examples/testing/messages.ts)
173-
- [Projects](examples/testing/projects.ts)
174-
- [Send mail using template](examples/testing/template.ts)
279+
- Templates CRUD – [`templates/everything.ts`](examples/templates/everything.ts)
175280

176-
### General API
281+
- Suppressions (find & delete) – [`sending/suppressions.ts`](examples/sending/suppressions.ts)
177282

178-
- [List User & Invite account accesses](examples/general/account-accesses.ts)
179-
- [Remove account access](examples/general/accounts.ts)
180-
- [Permissions](examples/general/permissions.ts)
283+
- Billing info – (no example yet) ← add in future
181284

182-
## Development
285+
- Accounts info – [`general/accounts.ts`](examples/general/accounts.ts)
183286

184-
This library is developed using [TypeScript](https://www.typescriptlang.org).
287+
- Permissions listing – [`general/permissions.ts`](examples/general/permissions.ts)
185288

186-
Use `yarn lint` to perform linting with [ESLint](https://eslint.org).
289+
- Users listing – [`general/account-accesses.ts`](examples/general/account-accesses.ts)
187290

188291
## Contributing
189292

0 commit comments

Comments
 (0)