GithubHelp home page GithubHelp logo

forwardemail / test-preview-emails-cross-browsers-ios-simulator-nodejs-javascript Goto Github PK

View Code? Open in Web Editor NEW
202.0 4.0 20.0 1.43 MB

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.

Home Page: https://forwardemail.net/docs/test-preview-email-rendering-browsers-ios-simulator

License: MIT License

JavaScript 71.55% Pug 27.38% Shell 1.07%
preview email letter opener previewer emails node pug render demo

test-preview-emails-cross-browsers-ios-simulator-nodejs-javascript's Introduction

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.

Need to send emails that land in the inbox instead of spam folder? Click here to learn how to send JavaScript contact forms and more with Node.js

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

test-preview-emails-cross-browsers-ios-simulator-nodejs-javascript's People

Contributors

niftylettuce avatar sibiraj-s avatar titanism avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

test-preview-emails-cross-browsers-ios-simulator-nodejs-javascript's Issues

Pugjs Uglifyjs dependency issues (Webpack)

node_modules/pug-filters/lib/run-filter.js 23:25-40
Critical dependency: the request of a dependency is an expression
node_modules/pug-filters/node_modules/uglify-js/tools/node.js 24:11-32
Critical dependency: the request of a dependency is an expression

Doesn't show on Ubuntu?

Hi,
first of all great work! We are using your email-templates module and the preview shows on every system except Ubuntu using Chrome.
Is that just us or a general issue?
All the best

[fix] preview-email is failing due to require('node:child_process') is not getting processed on mac intel

Describe the bug

Node.js version: 14.17.3

OS version: mac os 10.15.3

Description: require('node:child_process') throwing error

Actual behavior

Cannot find module 'node:child_process'

Expected behavior

preview-email should load properly

Code to reproduce

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Problem with WSL path

I am using this package through email-templates in a WSL environment. It opens chrome for the WSL as explained here, but it does not open the html generated file.

I specified a path like /mnt/c/... for dir option so both windows and WSL can access. It writes the html correctly, but does not load the page in chrome.

I think it is due to chrome on windows that does not understand WSL formatted path, I tested replacing this by:

if (options.open) await open(filePath.replace('/mnt/c', 'C:').replace('/', '\\'), options.open);

and it worked as expected (but it is not reliable at all).

I don't know if it is really an issue with this package or if it should be supported... Maybe just adding an option that takes a function to transform path to give to open could do the job at a minimal cost.

Styles overridden

It seems the styles in my HTML emails are being overridden by those of preview-email. Am I missing something?

Thanks for the great project!

Mailparser update

So it looks like a few hours ago nodemailer/mailparser reverted 3.0.1 back to MIT (yay!). And this version has the vulnerability fixed packages as well.

Dependency package of display-notification version 2.0.0 having vulnerability (Versions of the package semver before 7.5.2 are vulnerable to Regular Expression Denial of Service (ReDoS) via the function new Range)

Describe the bug

Node.js version: 18.16.1

OS version: Windows 10

Description: Dependency package of display-notification version 2.0.0 having vulnerability. Latest version of display-notification having fix. Link https://security.snyk.io/vuln/SNYK-JS-SEMVER-3247795

Actual behavior

Expected behavior

Code to reproduce

Checklist

  • [ *] I have searched through GitHub issues for similar issues.

  • [ *] I have completely read through the README and documentation.

  • [ *] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Support other template engines (handlebars, ejs...)

I'm using email-templates with hbs and I wonder if the preview package can support something else than pug?

Maybe override the default render function by one passed in options:

  options = {
    dir: os.tmpdir(),
    id: uuid.v4(),
    open: { wait: false },
    template: templateFilePath,
    renderFn: pug.renderFile,
    ...options
  };

....

  const html = await pify(options.renderFn)(
    options.template,
    Object.assign(options.locals, {
      cache: true,
      pretty: true,
      dayjs
    })
  );

[fix] Links in previews don't open on click

Describe the bug

Node.js version: v16.18.1

OS version: Fedora release 38 (Thirty Eight)

Description: Links in previews don't open on click. Is this an intentional security feature?

Actual behavior

Click on links in the email preview do not open after update from 3.0.10 to 3.0.17 (I assume due to ce6d443sandbox attribute).

Console error message, e.g. in chrome:

Blocked opening '<redacted>' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.

I can still work around this via "Open link in new tab" in the context menu (proving the link works in principle).

Expected behavior

Click on links in the email preview opens in new tab.

If no general safe solution can be found, maybe make the attribute value configurable by the (library) user.

Code to reproduce

file:///tmp/3aa0abe6-c509-4f8d-a359-a110c2facf8e.html opened in browser, rendering HTML preview of message incl.

…
<a href="http://localhost:3000/<redacted>" target="">Sign In</a>

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Browser doesn't open in WSL

I've tried several parameters for the app property both none worked.

Here's what I've tried:

  1. /c/Program Files (x86)/Google/Chrome/Application/chrome.exe (Nothing happens, I have a custom mounting for docker that's why I don't have the /mnt/ prefix)
  2. explorer.exe (Just opens a new Windows Explorer window)
  3. powershell.exe -c (Nothing happens)
  4. wsl-open (Nothing happens)

What's the reasoning behind adding google analytics in the template?

I've just discovered these lines in your email template:
https://github.com/niftylettuce/preview-email/blob/337f7f1007801a2691f427856a861808f7cbab62/template.pug#L169-L172

While I understand that you might be interested in gaining insights about the usage of this package, I feel that this is somewhat inappropriate in this context.
Even in a dev environment sensitive information might be displayed. And adding a third party script without an obvious disclaimer seems like a bad practice.

The result of email rendering is within <iframe> tage

Hi, I am using this preview-email to test if my template.pug display properly. When it render my template.pug in the message, everything from my template.pug is wrapped inside the <iframe>, and the font in the head was also in the iframe and not reflected in my email content. Can you have a look at it?

[fix] Define mailparser dependency with ~ instead of ^

Describe the bug

Node.js version: 14

OS version: macOS 12.6.1

Description: mailparser lib doesn't properly follow semver and has caused a breaking change in a minor/patch release.

Actual behavior

When running an application that depends upon preview-email, using node 14, the following error occurs:

./node_modules/mailparser/node_modules/html-to-text/lib/html-to-text.cjs:1838
    tableToString: (rows) => tableToString(rows, formatOptions.rowSpacing ?? 0, formatOptions.colSpacing ?? 3),
                                                                           ^
SyntaxError: Unexpected token '?'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/mailparser/lib/mail-parser.js:12:24)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/mailparser/index.js:3:20)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/preview-email/index.js:19:26)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/email-templates/lib/index.js:29:22)

Expected behavior

Expected to run without errors.

Code to fix

Update package.json with

    "mailparser": "~3.3.0",

or

    "mailparser": "~3.5.0",

Since 3.5.0 is the latest version that doesn't have breaking changes.

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
    No I have not done this because the issue is with Node 14.

Allow to configure the opn command

Hi, thank you for this great package.

I am working on Linux with visual code and recently upgraded from 1.27 to 1.28, after this upgrade the emails where not opened in the browser any more, but a window of visual code was opened.

I have found that it is because the text/html mime type was opened by default by visual code after the upgrade.
Before upgrade :

$ xdg-mime query default text/html
firefox.desktop
``
After upgrade : 

$ xdg-mime query default text/html
code-url-handler.desktop
``
I know it is not an issue with the email-preview package, but I wanted to expose the context because the xdg commands is used by opn.

So my need would be to have the opening command configurable so I can ask to specifically open firefox or google chrome.

Thank you very much.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.