Skip to content

Commit

Permalink
feat: added support for String and Buffer as first arg
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed May 16, 2023
1 parent 4c109ae commit 784cb50
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ Otherwise you will need to install XCode from the [App Store][app-store] or [App

> **NOTE**: You should probably just use [email-templates][] directly instead of using this package.
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).
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).

* The argument `message` can be one of the following:
* `Object` – A [Nodemailer message configuration](https://nodemailer.com/message/) object.
* `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/)).
* The argument `options` is documented under [Options](#options) below.

```js
const previewEmail = require('preview-email');
Expand Down
25 changes: 15 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const os = require('os');
const path = require('path');
const process = require('process');
const util = require('util');
const { Buffer } = require('buffer');
const displayNotification = require('display-notification');
const getPort = require('get-port');
const nodemailer = require('nodemailer');
Expand All @@ -13,7 +14,6 @@ const pEvent = require('p-event');
const pWaitFor = require('p-wait-for');
const pug = require('pug');
const uuid = require('uuid');
const { Iconv } = require('iconv');
const { isCI } = require('ci-info');
const { simpleParser } = require('mailparser');

Expand All @@ -40,17 +40,22 @@ const previewEmail = async (message, options) => {
simpleParser: {},
...options
};
debug('message', message, 'options', options);

if (typeof message !== 'object')
throw new Error('Message argument is required');
debug('message', message, 'options', options);

const response = await transport.sendMail(message);
let raw;
if (Buffer.isBuffer(message)) {
raw = message;
} else if (typeof message === 'string') {
raw = message;
} else if (typeof message === 'object') {
const response = await transport.sendMail(message);
raw = response.message;
} else {
throw new TypeError('Message argument is required');
}

const parsed = await simpleParser(response.message, {
...options.simpleParser,
Iconv
});
const parsed = await simpleParser(raw, options.simpleParser);

const html = await renderFilePromise(
options.template,
Expand Down Expand Up @@ -152,7 +157,7 @@ const previewEmail = async (message, options) => {
});

const emlFilePath = `${options.dir}/${options.id}.eml`;
await writeFile(emlFilePath, response.message);
await writeFile(emlFilePath, raw);
debug('emlFilePath', emlFilePath);
const xcrun = childProcess.spawn('xcrun', [
'simctl',
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"display-notification": "2.0.0",
"fixpack": "^4.0.0",
"get-port": "5.1.1",
"iconv": "^3.0.1",
"mailparser": "^3.6.4",
"nodemailer": "^6.9.2",
"open": "7",
Expand Down

0 comments on commit 784cb50

Please sign in to comment.