mailgun.js 3.5.6 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc CHANGED
@@ -22,6 +22,7 @@
22
22
  "settings": {
23
23
  "import/resolver": "webpack"
24
24
  },
25
+ "ignorePatterns": ["docs/assets/js/*", "dist/**"],
25
26
  "rules": {
26
27
  "tsdoc/syntax": "warn",
27
28
  "comma-dangle": 0,
@@ -32,6 +33,9 @@
32
33
  "class-methods-use-this": 0,
33
34
  "prefer-destructuring": 0,
34
35
  "import/extensions": 0,
35
- "lines-between-class-members": ["error", "always", { "exceptAfterSingleLine": true }]
36
+ "lines-between-class-members": ["error", "always", { "exceptAfterSingleLine": true }],
37
+ "@typescript-eslint/no-var-requires": {
38
+ "ignorePatterns": ["**/webpack/*.js"]
39
+ }
36
40
  }
37
- }
41
+ }
package/CHANGELOG.md CHANGED
@@ -2,6 +2,45 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [3.6.0](https://github.com/mailgun/mailgun-js/compare/v3.5.9...v3.6.0) (2021-10-13)
6
+
7
+
8
+ ### Features
9
+
10
+ * Split the distro for different targets ([1a55085](https://github.com/mailgun/mailgun-js/commits/1a550854765c7cdbb68b13b32c1f1392003abf30))
11
+ * Update commitlint rules ([262ce0c](https://github.com/mailgun/mailgun-js/commits/262ce0ccd594bbcd97c9aa9ba5f11ad6ce6fc8c5))
12
+
13
+ ### [3.5.9](https://github.com/mailgun/mailgun-js/compare/v3.5.8...v3.5.9) (2021-09-16)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * Update @commitlint/cli and standard-version dependencies ([fe0233e](https://github.com/mailgun/mailgun-js/commits/fe0233e9afbd546f33b0f1c73930ca9329b5fa38))
19
+
20
+
21
+ ### Other changes
22
+
23
+ * Add templates examples to readme ([f0bf0b8](https://github.com/mailgun/mailgun-js/commits/f0bf0b8cc66b3e7e5899fa913f2f79582f547a37))
24
+
25
+ ### [3.5.8](https://github.com/mailgun/mailgun-js/compare/v3.5.7...v3.5.8) (2021-09-02)
26
+
27
+
28
+ ### Features
29
+
30
+ * Update index.ts export to fix commonJs imports ([246c046](https://github.com/mailgun/mailgun-js/commits/246c046619c593e5f13abe1ff0414fe9c9524cf9))
31
+
32
+
33
+ ### Other changes
34
+
35
+ * Add examples of sending files to the readme ([a922b35](https://github.com/mailgun/mailgun-js/commits/a922b3590a6afd02e15650a2d6acf7b43933bb49))
36
+
37
+ ### [3.5.7](https://github.com/mailgun/mailgun-js/compare/v3.5.6...v3.5.7) (2021-08-16)
38
+
39
+
40
+ ### Bug Fixes
41
+
42
+ * Minification is messing up node-fetch's detection of abort signals ([79564f2](https://github.com/mailgun/mailgun-js/commits/79564f2ea46406e2df4ff911a8e49da44be86260))
43
+
5
44
  ### [3.5.6](https://github.com/mailgun/mailgun-js/compare/v3.5.5...v3.5.6) (2021-08-12)
6
45
 
7
46
 
package/README.md CHANGED
@@ -11,7 +11,7 @@ __Table of Contents__
11
11
  - [Setup Client](#setup-client)
12
12
  - [Methods](#methods)
13
13
  - [Browser Demo](#browser-demo)
14
- - [Examples](https://github.com/mailgun/mailgun-js/tree/c379f79ea2a2e0f825103751a3a102d8bdd3dd1b/example)
14
+ - [Examples](https://github.com/mailgun/mailgun-js/tree/master/examples)
15
15
  - [Development](#development)
16
16
  - [Requirements](#requirements)
17
17
  - [Build](#build)
@@ -125,42 +125,6 @@ Method naming conventions:
125
125
 
126
126
  `mg.messages.create(domain, data)` - [api docs](https://documentation.mailgun.com/api-sending.html#sending)
127
127
 
128
- HTML/TEXT Example:
129
-
130
- ```js
131
- mg.messages.create('sandbox-123.mailgun.org', {
132
- from: "Excited User <mailgun@sandbox-123.mailgun.org>",
133
- to: ["test@example.com"],
134
- subject: "Hello",
135
- text: "Testing some Mailgun awesomness!",
136
- html: "<h1>Testing some Mailgun awesomness!</h1>"
137
- })
138
- .then(msg => console.log(msg)) // logs response data
139
- .catch(err => console.log(err)); // logs any error
140
- ```
141
-
142
- MIME Example:
143
-
144
- ```js
145
- mg.messages.create('sandbox-123.mailgun.org', {
146
- from: "Excited User <mailgun@sandbox-123.mailgun.org>",
147
- to: ["test@example.com"],
148
- subject: "Hello",
149
- message: "<mime encoded string here>"
150
- })
151
- .then(msg => console.log(msg)) // logs response data
152
- .catch(err => console.log(err)); // logs any error
153
- ```
154
-
155
- Promise Returns:
156
-
157
- ```
158
- {
159
- id: '<20151025002517.117282.79817@sandbox-123.mailgun.org>',
160
- message: 'Queued. Thank you.'
161
- }
162
- ```
163
-
164
128
  Options:
165
129
 
166
130
  Parameter | Description
@@ -181,6 +145,248 @@ o:tracking-opens | Toggles opens tracking on a per-message basis. Has higher pr
181
145
  h:X-My-Header | h: prefix followed by an arbitrary value allows to append a custom MIME header to the message (X-My-Header in this case). For example, h:Reply-To to specify Reply-To address.
182
146
  v:my-var | v: prefix followed by an arbitrary name allows to attach a custom JSON data to the message. See Attaching Data to Messages for more information.
183
147
 
148
+
149
+ - HTML/TEXT Example:
150
+
151
+ ```js
152
+ mg.messages.create('sandbox-123.mailgun.org', {
153
+ from: "Excited User <mailgun@sandbox-123.mailgun.org>",
154
+ to: ["test@example.com"],
155
+ subject: "Hello",
156
+ text: "Testing some Mailgun awesomness!",
157
+ html: "<h1>Testing some Mailgun awesomness!</h1>"
158
+ })
159
+ .then(msg => console.log(msg)) // logs response data
160
+ .catch(err => console.log(err)); // logs any error
161
+ ```
162
+
163
+ - MIME Example:
164
+
165
+ ```js
166
+ mg.messages.create('sandbox-123.mailgun.org', {
167
+ from: "Excited User <mailgun@sandbox-123.mailgun.org>",
168
+ to: ["test@example.com"],
169
+ subject: "Hello",
170
+ text: "<mime encoded string here>"
171
+ })
172
+ .then(msg => console.log(msg)) // logs response data
173
+ .catch(err => console.log(err)); // logs any error
174
+ ```
175
+
176
+ Messages with attachments:
177
+
178
+ - Node.js example of send file as an attachment
179
+
180
+ ```js
181
+ const fsPromises = require('fs').promises;
182
+ const path = require('path');
183
+ const filepath = path.resolve(__dirname, '../test.pdf');
184
+ let messageParams = {
185
+ from: "Excited User <mailgun@sandbox-123.mailgun.org>",
186
+ to: ["test@example.com"],
187
+ subject: "Test subject",
188
+ text: "Hello here is a file in the attachment"
189
+ }
190
+
191
+ fsPromises.readFile(filepath)
192
+ .then(data => {
193
+ const file = {
194
+ filename: 'test-rename.pdf',
195
+ data
196
+ }
197
+ messageParams.attachment = file;
198
+ return mg.messages.create('sandbox-123.mailgun.org', messageParams);
199
+ })
200
+ .then(response => {
201
+ console.log(response);
202
+ })
203
+ ```
204
+ - Node.js example of send multiple files as an attachment
205
+ ```js
206
+ const fsPromises = require('fs').promises;
207
+ const path = require('path');
208
+ const filepath = path.resolve(__dirname, '../test.pdf');
209
+ const filepath1 = path.resolve(__dirname, '../test.jpg');
210
+
211
+ let messageParams = {
212
+ from: "Excited User <mailgun@sandbox-123.mailgun.org>",
213
+ to: ["test@example.com"],
214
+ subject: "Test subject",
215
+ text: "Test message"
216
+ }
217
+
218
+ (async () =>{
219
+ try {
220
+ const firstFile = {
221
+ filename: 'test.pdf',
222
+ data: await fsPromises.readFile(filepath)
223
+ }
224
+
225
+ const secondFile = {
226
+ filename: 'test.jpg',
227
+ data: await fsPromises.readFile(filepath1)
228
+ }
229
+
230
+ messageParams.attachment = [firstFile, secondFile];
231
+ const result = await mg.messages.create('sandbox-123.mailgun.org', messageParams);
232
+ console.log(result);
233
+ } catch (error) {
234
+ console.error(error);
235
+ }
236
+ })()
237
+ ```
238
+ - Node.js example of send file as inline image
239
+ ```js
240
+ const fsPromises = require('fs').promises;
241
+ const path = require('path');
242
+ const filepath = path.resolve(__dirname, '../test.jpg');
243
+ let messageParams = {
244
+ from: "Excited User <mailgun@sandbox-123.mailgun.org>",
245
+ to: ["test@example.com"],
246
+ subject: "Test subject",
247
+ html: '<div><img alt="image" id="1" src="cid:test.jpg"/></div> Some extra text'
248
+ }
249
+
250
+ fsPromises.readFile(filepath)
251
+ .then(data => {
252
+ const file = {
253
+ filename: 'test.jpg',
254
+ data
255
+ }
256
+
257
+ messageParams.inline = file;
258
+ return mg.messages.create('sandbox-123.mailgun.org', messageParams);
259
+ })
260
+ .then(response => {
261
+ console.log(response);
262
+ })
263
+ ```
264
+
265
+ - Browser example of send file
266
+
267
+ Before sending the file you need to somehow get the Blob of the file.
268
+ Usually can get it from the onChange event of input tag with type file.
269
+ ```js
270
+ const handleFileSelected = async (event) => {
271
+ const files = Array.from(event.target.files)
272
+ const fileBuffer = await files[0];
273
+ }
274
+ <input type="file" onChange={handleFileSelected} name="file-uploader"/>
275
+ ```
276
+
277
+ Then you can use the same approach as shown above for node.js apps.
278
+ ```js
279
+ const file = {
280
+ filename: 'test.pdf',
281
+ data: fileBuffer
282
+ };
283
+
284
+ let messageParams = {
285
+ from: "Excited User <mailgun@sandbox-123.mailgun.org>",
286
+ to: ["test@example.com"],
287
+ subject: "Test subject",
288
+ text: "Hello here is a file in the attachment",
289
+ attachment: file
290
+ };
291
+
292
+ const res = await mg.messages.create(DOMAIN, messageParams);
293
+ ```
294
+ Promise Returns:
295
+
296
+ ```js
297
+ {
298
+ id: '<20151025002517.117282.79817@sandbox-123.mailgun.org>',
299
+ message: 'Queued. Thank you.'
300
+ }
301
+ ```
302
+ #### Templates
303
+
304
+ Mailgun’s templates uses a fork of the very popular template engine [handlebars](https://handlebarsjs.com/).
305
+
306
+ To provide values for a substitution you need to use 'h:X-Mailgun-Variables' property in the message description.
307
+
308
+ Make sure that this property is a JSON string like {"title":"A title", "body":"The body"}.
309
+
310
+ You can find few examples of how to use templates below.
311
+ - Providing values for **title** and **slug** variables to render in template
312
+ ```js
313
+ ...
314
+ const {
315
+ title,
316
+ slug,
317
+ } = someDataSource;
318
+
319
+ const mailgunData = {
320
+ from: 'mailer@example.com>',
321
+ to: 'recipient@example.com',
322
+ subject: `Email ${title}`,
323
+ template: 'name-of-the-template-you-made-in-mailgun-web-portal',
324
+ 'h:X-Mailgun-Variables': JSON.stringify({ // be sure to stringify your payload
325
+ title,
326
+ slug,
327
+ }),
328
+ 'h:Reply-To': 'reply-to@example.com',
329
+ };
330
+
331
+ try {
332
+ const response = await mailgun.messages.create(DOMAIN_NAME, mailgunData);
333
+ ...
334
+ ```
335
+
336
+ - Providing an array of objects to render them in the template
337
+ ```JS
338
+ ...
339
+ const mailgunData = {
340
+ from: 'mailer@example.com>',
341
+ to: 'recipient@example.com',
342
+ subject: `Email ${title}`,
343
+ template: 'name-of-the-another-template-you-made-in-mailgun-web-portal',
344
+ 'h:X-Mailgun-Variables': JSON.stringify({
345
+ "arrayItems": [
346
+ {
347
+ "question": "test_question",
348
+ "answer": "test_answer"
349
+ },
350
+ {
351
+ "question": "test_question",
352
+ "answer": "test_answer"
353
+ }
354
+ ]})
355
+ };
356
+ try {
357
+ const response = await mailgun.messages.create(DOMAIN_NAME, mailgunData);
358
+ ...
359
+ ```
360
+
361
+ #### Recipient Variables
362
+ [Docs](https://documentation.mailgun.com/en/latest/user_manual.html#batch-sending)
363
+
364
+ Recipient Variables are custom variables that you define, which you can then reference in the message body. They give you the ability to send a custom message to each recipient while still using a single API Call.
365
+
366
+ ```Js
367
+ ...
368
+ const mailgunData = {
369
+ from: 'Example.com Mailer <mailer@mailer.example.com>',
370
+ to: ['me@example.com', 'you@example.com'],
371
+ subject: 'Recipient - %recipient.title%',
372
+ html: 'Here\'s %recipient.title% and <a href="%recipient.link%">link</a>',
373
+ 'recipient-variables': JSON.stringify({
374
+ 'me@example.com': {
375
+ title: 'Me',
376
+ link: 'href-var',
377
+ },
378
+ 'you@example.com': {
379
+ title: 'You',
380
+ link: 'slug-recipient-var-c',
381
+ },
382
+ }),
383
+ };
384
+
385
+ try {
386
+ const response = await mailgun.messages.create(DOMAIN_NAME, mailgunData);
387
+ ...
388
+ ```
389
+
184
390
  ### domains
185
391
 
186
392
  #### list
@@ -1,8 +1,7 @@
1
1
  module.exports = {
2
2
  extends: ['@commitlint/config-conventional'],
3
3
  rules: {
4
- 'type-enum': [2, 'always', ['Breaking', 'Feature', 'Fix', 'Other']],
5
- 'type-case': [2, 'always', 'pascal-case'],
4
+ 'type-enum': [2, 'always', ['breaking', 'feature', 'fix', 'other']],
6
5
  'subject-case': [2, 'always', 'sentence-case']
7
6
  },
8
7
  };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import Client from './lib/client';
1
2
  import Options from './lib/interfaces/Options';
2
3
  import IFormData from './lib/interfaces/IFormData';
3
- export default class Mailgun {
4
+ declare class Mailgun {
4
5
  private formData;
5
6
  constructor(FormData: new (...args: any[]) => IFormData);
6
- client(options: Options): any;
7
+ client(options: Options): Client;
7
8
  }
9
+ export = Mailgun;