mailgen
A Node.js package that generates clean, responsive HTML e-mails for sending...
README
Programmatically create beautiful e-mails using plain old JavaScript.
Demo

These e-mails were generated using the built-in salted theme.
Usage
- ``` sh
- npm install mailgen --save
- ```
- ``` js
- var Mailgen = require('mailgen');
- // Configure mailgen by setting a theme and your product info
- var mailGenerator = new Mailgen({
- theme: 'default',
- product: {
- // Appears in header & footer of e-mails
- name: 'Mailgen',
- link: 'https://mailgen.js/'
- // Optional product logo
- // logo: 'https://mailgen.js/img/logo.png'
- }
- });
- ```
- ``` js
- var email = {
- body: {
- name: 'John Appleseed',
- intro: 'Welcome to Mailgen! We\'re very excited to have you on board.',
- action: {
- instructions: 'To get started with Mailgen, please click here:',
- button: {
- color: '#22BC66', // Optional action button color
- text: 'Confirm your account',
- link: 'https://mailgen.js/confirm?s=d9729feb74992cc3482b350163a1a010'
- }
- },
- outro: 'Need help, or have questions? Just reply to this email, we\'d love to help.'
- }
- };
- // Generate an HTML email with the provided contents
- var emailBody = mailGenerator.generate(email);
- // Generate the plaintext version of the e-mail (for clients that do not support HTML)
- var emailText = mailGenerator.generatePlaintext(email);
- // Optionally, preview the generated HTML e-mail by writing it to a local file
- require('fs').writeFileSync('preview.html', emailBody, 'utf8');
- // `emailBody` now contains the HTML body,
- // and `emailText` contains the textual version.
- //
- // It's up to you to send the e-mail.
- // Check out nodemailer to accomplish this:
- // https://nodemailer.com/
- ```

More Examples
Plaintext E-mails
- ``` js
- // Generate plaintext email using mailgen
- var emailText = mailGenerator.generatePlaintext(email);
- ```
Supported Themes




Custom Themes
RTL Support
- ``` js
- var mailGenerator = new Mailgen({
- theme: 'salted',
- // Custom text direction
- textDirection: 'rtl',
- });
- ```
Custom Logo Height
- ``` js
- var mailGenerator = new Mailgen({
- product: {
- // Custom product logo URL
- logo: 'https://mailgen.js/img/logo.png',
- // Custom logo height
- logoHeight: '30px'
- }
- });
- ```
Language Customizations
- ``` js
- var email = {
- body: {
- greeting: 'Dear',
- signature: 'Sincerely'
- }
- };
- ```
- ``` js
- var email = {
- body: {
- signature: false,
- greeting: false // This will override and disable name & title options
- }
- };
- ```
- ``` js
- var email = {
- body: {
- // Title will override `name`
- title: 'Welcome to Mailgen!'
- }
- };
- ```
- ``` js
- // Configure mailgen
- var mailGenerator = new Mailgen({
- theme: 'salted',
- product: {
- name: 'Mailgen',
- link: 'https://mailgen.js/',
- // Custom copyright notice
- copyright: 'Copyright © 2016 Mailgen. All rights reserved.',
- }
- });
- ```
Multiline Support
- ``` js
- var email = {
- body: {
- intro: ['Welcome to Mailgen!', 'We\'re very excited to have you on board.'],
- outro: ['Need help, or have questions?', 'Just reply to this email, we\'d love to help.'],
- }
- };
- ```
Elements
Action
- ``` js
- var email = {
- body: {
- action: {
- instructions: 'To get started with Mailgen, please click here:',
- button: {
- color: '#48cfad', // Optional action button color
- text: 'Confirm your account',
- link: 'https://mailgen.js/confirm?s=d9729feb74992cc3482b350163a1a010'
- }
- }
- }
- };
- ```
- ``` js
- var email = {
- body: {
- action: [
- {
- instructions: 'To get started with Mailgen, please click here:',
- button: {
- color: '#22BC66',
- text: 'Confirm your account',
- link: 'https://mailgen.js/confirm?s=d9729feb74992cc3482b350163a1a010'
- }
- },
- {
- instructions: 'To read our frequently asked questions, please click here:',
- button: {
- text: 'Read our FAQ',
- link: 'https://mailgen.js/faq'
- }
- }
- ]
- }
- };
- ```
- ``` js
- var email = {
- body: {
- action: [
- {
- instructions: 'To get started with Mailgen, please click here:',
- button: {
- color: '#22BC66',
- text: 'Confirm your account',
- link: 'https://mailgen.js/confirm?s=d9729feb74992cc3482b350163a1a010',
- fallback: true
- }
- },
- {
- instructions: 'To read our frequently asked questions, please click here:',
- button: {
- text: 'Read our FAQ',
- link: 'https://mailgen.js/faq',
- fallback: {
- text: 'This is my custom text for fallback'
- }
- }
- }
- ]
- }
- };
- ```
Table
- ``` js
- var email = {
- body: {
- table: {
- data: [
- {
- item: 'Node.js',
- description: 'Event-driven I/O server-side JavaScript environment based on V8.',
- price: '$10.99'
- },
- {
- item: 'Mailgen',
- description: 'Programmatically create beautiful e-mails using plain old JavaScript.',
- price: '$1.99'
- }
- ],
- columns: {
- // Optionally, customize the column widths
- customWidth: {
- item: '20%',
- price: '15%'
- },
- // Optionally, change column text alignment
- customAlignment: {
- price: 'right'
- }
- }
- }
- }
- };
- ```
- ``` js
- var email = {
- body: {
- table: [
- {
- // Optionally, add a title to each table.
- title: 'Order 1',
- data: [
- {
- item: 'Item 1',
- description: 'Item 1 description',
- price: '$1.99'
- },
- {
- item: 'Item 2',
- description: 'Item 2 description',
- price: '$2.99'
- }
- ],
- columns: {
- // Optionally, customize the column widths
- customWidth: {
- item: '20%',
- price: '15%'
- },
- // Optionally, change column text alignment
- customAlignment: {
- price: 'right'
- }
- }
- },
- {
- // Optionally, add a title to each table.
- title: 'Order 2',
- data: [
- {
- item: 'Item 1',
- description: 'Item 1 description',
- price: '$2.99'
- },
- {
- item: 'Item 2',
- description: 'Item 2 description',
- price: '$1.99'
- }
- ],
- columns: {
- // Optionally, customize the column widths
- customWidth: {
- item: '20%',
- price: '15%'
- },
- // Optionally, change column text alignment
- customAlignment: {
- price: 'right'
- }
- }
- }
- ]
- }
- };
- ```
Note: Tables are currently not supported in plaintext versions of e-mails.
Dictionary
- ``` js
- var email = {
- body: {
- dictionary: {
- date: 'June 11th, 2016',
- address: '123 Park Avenue, Miami, Florida'
- }
- }
- };
- ```
Go-To Actions
- ``` js
- var email = {
- body: {
- // Optionally configure a Go-To Action button
- goToAction: {
- text: 'Go to Dashboard',
- link: 'https://mailgen.com/confirm?s=d9729feb74992cc3482b350163a1a010',
- description: 'Check the status of your order in your dashboard'
- }
- }
- };
- ```
Note that you need to get your sender address whitelisted before your Go-To Actions will show up in Gmail.
Troubleshooting
Simply sending the X-Entity-Ref-ID header with your e-mails will prevent grouping / truncation.
Contributing
Check out THEME.md if you want to add a new built-in theme to Mailgen.
探客时代
