Skip to content

Commit 784cb50

Browse files
committed
feat: added support for String and Buffer as first arg
1 parent 4c109ae commit 784cb50

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ Otherwise you will need to install XCode from the [App Store][app-store] or [App
6161

6262
> **NOTE**: You should probably just use [email-templates][] directly instead of using this package.
6363
64-
The function `previewEmail` returns a `Promise` which resolves with a URL. We automatically open the browser to this URL unless you specify `options.open` as `false` (see [Options](#options) for more info).
64+
The function `previewEmail` accepts two arguments `message` and `options`, and it returns a `Promise` which resolves with a URL (unless you specify `returnHTML: true` in `options` argument). We automatically open the browser to this URL unless you specify `options.open` as `false` (see [Options](#options) for more info).
65+
66+
* The argument `message` can be one of the following:
67+
* `Object` – A [Nodemailer message configuration](https://nodemailer.com/message/) object.
68+
* `String` or `Buffer` – A custom generated RFC822 formatted message to use (instead of one that is generated by Nodemailer – see [Nodemailer's custom source](https://nodemailer.com/message/custom-source/)).
69+
* The argument `options` is documented under [Options](#options) below.
6570

6671
```js
6772
const previewEmail = require('preview-email');

index.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const os = require('os');
55
const path = require('path');
66
const process = require('process');
77
const util = require('util');
8+
const { Buffer } = require('buffer');
89
const displayNotification = require('display-notification');
910
const getPort = require('get-port');
1011
const nodemailer = require('nodemailer');
@@ -13,7 +14,6 @@ const pEvent = require('p-event');
1314
const pWaitFor = require('p-wait-for');
1415
const pug = require('pug');
1516
const uuid = require('uuid');
16-
const { Iconv } = require('iconv');
1717
const { isCI } = require('ci-info');
1818
const { simpleParser } = require('mailparser');
1919

@@ -40,17 +40,22 @@ const previewEmail = async (message, options) => {
4040
simpleParser: {},
4141
...options
4242
};
43-
debug('message', message, 'options', options);
4443

45-
if (typeof message !== 'object')
46-
throw new Error('Message argument is required');
44+
debug('message', message, 'options', options);
4745

48-
const response = await transport.sendMail(message);
46+
let raw;
47+
if (Buffer.isBuffer(message)) {
48+
raw = message;
49+
} else if (typeof message === 'string') {
50+
raw = message;
51+
} else if (typeof message === 'object') {
52+
const response = await transport.sendMail(message);
53+
raw = response.message;
54+
} else {
55+
throw new TypeError('Message argument is required');
56+
}
4957

50-
const parsed = await simpleParser(response.message, {
51-
...options.simpleParser,
52-
Iconv
53-
});
58+
const parsed = await simpleParser(raw, options.simpleParser);
5459

5560
const html = await renderFilePromise(
5661
options.template,
@@ -152,7 +157,7 @@ const previewEmail = async (message, options) => {
152157
});
153158

154159
const emlFilePath = `${options.dir}/${options.id}.eml`;
155-
await writeFile(emlFilePath, response.message);
160+
await writeFile(emlFilePath, raw);
156161
debug('emlFilePath', emlFilePath);
157162
const xcrun = childProcess.spawn('xcrun', [
158163
'simctl',

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"display-notification": "2.0.0",
1616
"fixpack": "^4.0.0",
1717
"get-port": "5.1.1",
18-
"iconv": "^3.0.1",
1918
"mailparser": "^3.6.4",
2019
"nodemailer": "^6.9.2",
2120
"open": "7",

0 commit comments

Comments
 (0)