diff --git a/README.md b/README.md index 86efeb4..17b8afe 100644 --- a/README.md +++ b/README.md @@ -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'); diff --git a/index.js b/index.js index 58bf285..2a5e319 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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'); @@ -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, @@ -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', diff --git a/package.json b/package.json index a239069..261bdfd 100644 --- a/package.json +++ b/package.json @@ -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",