gkampitakis / email-client

A wrapper for sending email with major email providers

Home Page:https://www.npmjs.com/package/template-email-client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Email Client

A library written in typescript using known email clients for sending emails. It has built in support for compiling templates and sending html in the email.

Usage

Configuration of client

// or with commonjs const EmailClient = require('template-email-client');

import { EmailClient } from 'template-email-client';

const client = new EmailClient({
    transporter: 'sendgrid', // Supported 'sendgrid', 'mailgun', 'postmark', 'SES'
    apiKey: '', //Your send grid api key
    templateLanguage: 'handlebars', // Supported 'handlebars', 'mjml', 'ejs'
    production: true, // or process.env.NODE_ENV = production is as setting to true
    tmpltCacheSize: 50, // template cache size default = 100
    attCacheSize: 50, // attachment cache size default = 100
});

Note that in production mode either by explicitly setting it to true or by setting NODE_ENV = production template-email-client will cache template and attachment files.

Configurations for Transporters

Sendgrid Config

{
  "transporter": "sendgrid",
  "apiKey": "*******",
  "templateLanguage": "handlebars" // Supported 'handlebars', 'mjml', 'ejs'
}

Mailgun Config

{
  "transporter": "mailgun",
  "apiKey": "*******",
  "domain": "/mock/domain",
  1"templateLanguage": "handlebars" // Supported 'handlebars', 'mjml', 'ejs'
}

Postmark Config

{
  "transporter": "postmark",
  "serverToken": "*******",
  "configOptions": {},
  "templateLanguage": "handlebars" // Supported 'handlebars', 'mjml', 'ejs'
}

AWS Config

{
  "transporter": "SES",
  "accessKeyId": "*******",
  "secretAccessKey": "*******",
  "region:": "eu-west-2",
  "templateLanguage": "handlebars" // Supported 'handlebars', 'mjml', 'ejs'
}

Send an email

client.send({
    from: 'mock@email.com',
    to: 'test@email.com',
    text: 'Hello World',
});

API

  • from string
  • to string or string []
  • cc string or string []
  • bcc string or string []
  • text string
  • html string
  • subject string
  • template string the path of the html template you want to use
  • data object an object containing the data that the template is going to be compiled with
  • subject string
  • any other transporter specific field
  • attachments
[
    {
        name: 'myfilte.txt', // optional if not provided take filename
        path: __dirname + '/path/to/file',
    },
][
    // or
    (__dirname + '/path/to/file', 'another/file')
];

Email Client Methods

const client = new EmailClient({...});

/*Exposes handlebars configuration method*/
client.configureHandlebars({
	configure:()=>void,
	helpers:[]
});

/*Set a new transporter*/
client.setTransporter('sendgrid',{...});

/*Get the transporter*/
client.getTransporter();

Supported Templates

  • mjml
  • handlebars
  • ejs

Supported Clients

  • mailgun
  • postmark
  • sendgrid
  • SES

Example

You can also check an example usage.

Issues

For any issues.

License

MIT License

About

A wrapper for sending email with major email providers

https://www.npmjs.com/package/template-email-client

License:MIT License


Languages

Language:TypeScript 97.3%Language:JavaScript 2.1%Language:Handlebars 0.5%Language:EJS 0.1%