• Stars
    star
    196
  • Rank 198,553 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatically opens your browser and iOS Simulator to preview Node.js email messages sent with Nodemailer. Made for @forwardemail and @ladjs. Cross-browser and cross-platform email testing.

preview-email

build status code style styled with prettier made with lass license

Automatically opens your browser and iOS Simulator to preview Node.js email messages sent with Nodemailer. Made for Forward Email and Lad. Cross-platform and cross-browser email testing.

Table of Contents

Screenshots

iOS Simulator

macOS Notification Screenshot
iOS Simulator Screenshot

Browser

Browser Screenshot

Install

npm:

npm install preview-email

Requirements

As of v3.0.6 we have built-in support for previewing emails in the iOS Simulator (in addition to rendering them in your default web browser).

This is only applicable if you are using macOS and if not running in a CI environment. If you wish to disable this default behavior, then set openSimulator to false in the options.

Otherwise you will need to install XCode from the App Store or Apple Developer Website. We have built-in friendly macOS notifications that will alert you if there are any issues while attempting to load the iOS Simulator.

After installing XCode, you will need to open it and agree to the terms and conditions. Then you will need to assign Command Line Tools.

Once the Simulator is opened – if you need to inspect the rendered email, then you can use the Web Inspector in Safari Developer Tools.

Usage

NOTE: You should probably just use email-templates directly instead of using this package.

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 for more info).

const previewEmail = require('preview-email');
const nodemailer = require('nodemailer');

const transport = nodemailer.createTransport({
  jsonTransport: true
});

// <https://nodemailer.com/message/>
const message = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Hello world',
  html: '<p>Hello world</p>',
  text: 'Hello world',
  attachments: [{ filename: 'hello-world.txt', content: 'Hello world' }]
};

// note that `attachments` will not be parsed unless you use
// `previewEmail` with the results of `transport.sendMail`
// e.g. `previewEmail(JSON.parse(res.message));` where `res`
// is `const res = await transport.sendMail(message);`
previewEmail(message).then(console.log).catch(console.error);

transport.sendMail(message).then(console.log).catch(console.error);

Custom Preview Template and Stylesheets

Using the options.template object, you can define your own template for rendering (e.g. get inspiration from template.pug and write your own!):

const path = require('path');

// ...

previewEmail(message, {
  template: path.join(__dirname, 'my-custom-preview-template.pug')
})
  .then(console.log)
  .catch(console.error);

Debugging

You can easily debug output from preview-email:

NODE_DEBUG=preview-email node app.js

Options

  • message (Object) - a Nodemailer message configuration object
  • options (Object) - an object with the following two properties:
    • id (String) - a unique ID for the file name created for the preview in dir (defaults to uuid.v4() from uuid)
    • dir (String) - a path to a directory for saving the generated email previews (defaults to os.tmpdir(), see os docs for more insight)
    • open (Object or Boolean) - an options object that is passed to open (defaults to { wait: false }) - if set to false then it will not open the email automatically in the browser using open, and if set to true then it will default to { wait: false }
    • template (String) - a file path to a pug template file (defaults to preview-email's template.pug by default) - this is where you can pass a custom template for rendering email previews, e.g. your own stylesheet
    • urlTransform (Function (path) => url) - a function to build preview url from file path (defaults to (path) => 'file://[file path]') - this is where you can customize the opened path to handle WSL to Windows transformation or build a http url if dir is served.
    • openSimulator (Boolean) - whether or not to open the iOS Simulator with the preview url file path (defaults to true via process.env.NODE_ENV !== 'test' and will only run if macOS detected and not in a CI environment)
    • simpleParser (Object) - an options Object to pass to mailparser's simpleParser method (see mailparser docs for available options – note that Iconv option is always overridden for safeguard)
    • returnHTML (Boolean) - whether or not to return HTML only – and subsequently not write nor open the file preview file (defaults to false)
    • hasDownloadOriginalButton (Boolean) - whether or not to render a "Download Original" button to download via base64 inline onclick JavaScript (defaults to true)

License

MIT © Forward Email

More Repositories

1

email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
JavaScript
3,594
star
2

free-email-forwarding

The best free email forwarding for custom domains. Visit our website to get started (SMTP server)
JavaScript
2,370
star
3

forwardemail.net

Privacy-focused encrypted email for everyone.
JavaScript
663
star
4

reserved-email-addresses-list

List of 1250+ generic, admin, mailer-daemon, and no-reply usernames reserved for security concerns. Made for @forwardemail.
JavaScript
144
star
5

font-awesome-assets

💄 Convert any of @FortAwesome's Font-Awesome icons to an asset, such as an <svg> tag or a Base64-encoded PNG/SVG <img> tag! It supports Retina devices and custom tag attributes too!
HTML
130
star
6

custom-fonts-in-emails

An extremely easy way to use custom fonts in emails without having to use art software.
JavaScript
119
star
7

nodejs-dns-over-https-tangerine

Node.js DNS over HTTPS - 🍊 Tangerine is the best drop-in replacement for dns.promises.Resolver using DNS over HTTPS ("DoH") via undici with built-in retries, timeouts, smart server rotation, AbortControllers, and caching support for multiple backends (with TTL and purge support). Made for @forwardemail.
JavaScript
107
star
8

nodemailer-base64-to-s3

⚡ Convert your Base64-Encoded Data URI's in `<img>` tags to Amazon S3/CloudFront URL's. A better alternative than embedding images with CID using Nodemailer.
JavaScript
30
star
9

status.forwardemail.net

Status Page
Markdown
13
star
10

get-fqdn

Lookup the fully qualified domain name ("FQDN") of the current server's IP (default) or a custom IP. 90x faster than `hostname -f` and works with Node v6.4+.
JavaScript
6
star
11

authheaders

Node.js wrapper around the Python pip package authheaders exposing a function to generate Authentication-Results headers
JavaScript
5
star
12

python-spfcheck2

Node.js wrapper around Python's spf.check2 function which conforms to both RFC4408 and RFC7208
JavaScript
5
star
13

browser-extension

The best privacy-focused browser extension with one-click disposable addresses, unlimited email aliases, and access to your Forward Email account.
3
star
14

dkimpy

Node.js wrapper around the Python pip package dkimpy exposing DKIM and ARC signing and verification functions
JavaScript
2
star
15

python-dkim-verify

Node.js wrapper around Python's dkim.verify function which conforms to RFC spec
JavaScript
2
star
16

python-arf

Node.js wrapper around the Python package arf, which is a processor for Abuse Reporting Format (ARF) messages.
JavaScript
2
star
17

authentication-headers

Our independently maintained fork of @adam-iris authentication-headers repo which contains a fix in the Python package "authheaders".
Python
1
star
18

mta-sts.forwardemail.net

HTML
1
star