0.2.1 • Published 2 years ago
@sempervirens/emailer v0.2.1
Sempervirens Emailer
A wrapper for Nodemailer, it provides a simplified interface for sending emails.
Features
- Checks if the
body
parameter is HTML or plain text, and if plain text, it adds the plain text to the email in case the recipient client is not HTML compatible. - Enables creating an Emailer instance that can be pre-configured and used across the process.
- Enables calling only
send
without pre-configuring. - Provides Gmail as the default service.
Installation
npm i @sempervirens/emailer
Usage
Quick Start
Setup a Gmail account with an app password.
Import
Emailer
.(Optional) Initialize
emailer
withname
,from
, andpassword
so those parameters may be omitted when callingsend
.Call send, with or without
name
,from
, andpassword
.
Note: If initialized, it is only necessary to pass name
, from
, and password
into emailer
once. Then importing emailer
anywhere else, it is only necessary to call send
.
import emailer from '@sempervirens/emailer';
(async () => {
// Without initializing
const data1 = await Emailer.send({
name: 'From name',
from: 'From email',
password: 'Gmail app password',
to: 'To email',
subject: 'Email subject',
body: 'Email text or HTML'
});
// With initializing
const emailer = new Emailer({
name: 'From name',
from: 'From email',
password: 'Gmail app password'
});
// In a file somewhere else
const data2 = await emailer.send({
to: 'To email',
subject: 'Email subject',
body: 'Email text or HTML'
});
})();
Advanced
const data3 = await Emailer.send({
options: {
service: 'gmail',
auth: {
user: 'From email',
auth: 'Gmail app password'
}
},
to: 'To email',
subject: 'Email subject',
body: 'Email text or HTML'
});
API
constructor
Param | Type | Description |
---|---|---|
name | string | Required if options not given. From name to be displayed with from email. |
from | string | Required if options not given. From email. |
password | string | Required if options not given. From email password or app password. |
options | object | Required if name , from , password not given. Nodemailer options. |
send (static or instance)
Note: If using the static function, name
, from
, password
, etc. are required.
Param | Type | Description |
---|---|---|
name | string | Required if static and options not given. From name to be displayed with from email. |
from | string | Required if static and options not given. From email. |
password | string | Required if static and options not given. From email password or app password. |
options | object | Required if static and name , from , password not given. Nodemailer options. |
to | string | Required. The to email address. |
subject | string | Recommended. The email subject. |
body | string | Recommended. HTML or plain text email body. |