What is preview-email?
The preview-email npm package allows developers to preview email templates during development. It is particularly useful for testing and debugging email content before sending it to actual recipients.
What are preview-email's main functionalities?
Preview Email in Browser
This feature allows you to preview an email in the browser. The email object contains the necessary fields like 'from', 'to', 'subject', and 'html'. The previewEmail function generates a preview URL that you can open in your browser.
const previewEmail = require('preview-email');
const email = {
from: 'sender@example.com',
to: 'recipient@example.com',
subject: 'Test Email',
html: '<h1>Hello World</h1>'
};
previewEmail(email).then(console.log).catch(console.error);
Custom Template Rendering
This feature allows you to use custom template engines like EJS to render dynamic email content. The rendered HTML is then passed to the previewEmail function for previewing.
const previewEmail = require('preview-email');
const ejs = require('ejs');
const emailTemplate = '<h1>Hello <%= name %></h1>';
const emailData = { name: 'John Doe' };
const emailHtml = ejs.render(emailTemplate, emailData);
const email = {
from: 'sender@example.com',
to: 'recipient@example.com',
subject: 'Personalized Email',
html: emailHtml
};
previewEmail(email).then(console.log).catch(console.error);
Inline CSS
This feature allows you to inline CSS styles in your email HTML using the 'juice' library. The inlined HTML is then passed to the previewEmail function for previewing.
const previewEmail = require('preview-email');
const juice = require('juice');
const emailHtml = '<style>h1 { color: red; }</style><h1>Hello World</h1>';
const inlinedHtml = juice(emailHtml);
const email = {
from: 'sender@example.com',
to: 'recipient@example.com',
subject: 'Styled Email',
html: inlinedHtml
};
previewEmail(email).then(console.log).catch(console.error);
Other packages similar to preview-email
nodemailer
Nodemailer is a module for Node.js applications to allow easy email sending. While it focuses on sending emails, it also provides a plugin system that can be used to preview emails. However, it is more complex and feature-rich compared to preview-email.
maildev
MailDev is a simple way to test email sending during development. It acts as a local SMTP server and web interface for viewing emails. Unlike preview-email, MailDev provides a full SMTP server and a web interface for viewing emails.
email-templates
Email-templates is a Node.js module for rendering email templates. It supports various template engines and inline CSS. While it focuses on rendering and sending emails, it also provides a preview feature. It is more comprehensive compared to preview-email.
preview-email
Automatically opens your browser to preview Node.js email messages sent with Nodemailer. Made for Lad!
Table of Contents
Screenshot
Install
npm:
npm install preview-email
yarn:
yarn add preview-email
Usage
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 for more info).
const previewEmail = require('preview-email');
const nodemailer = require('nodemailer');
const transport = nodemailer.createTransport({
jsonTransport: true
});
const message = {
from: 'niftylettuce+from@gmail.com',
to: 'niftylettuce+to@gmail.com',
subject: 'Hello world',
html: '<p>Hello world</p>',
text: 'Hello world',
attachments: [ { filename: 'hello-world.txt', content: 'Hello world' } ]
};
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
Thanks to the debug package, you can easily debug output from preview-email
:
DEBUG=preview-email node app.js
Options
message
(Object) - a Nodemailer message configuration objectoptions
(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 automaitcally 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
Contributors
License
MIT © Nick Baugh