Mailing basics with modularity.
npm install @universal-packages/mailing


Mailing basics with modularity.
``shell`
npm install @universal-packages/mailing
Mailing is the main interface for email sending, it uses an email engine and renderer to general email contents and send them over the internet.
`js
import { Mailing } from '@universal-packages/mailing'
import { NodemailerEngine } from '@universal-packages/mailing-nodemailer'
const mailing = new Mailing( engine: 'nodemailer', engineOptions: { transport: 'smtp', options: { host: 'smtp.com'} })
await mailing.prepare()
mailing.send({ subject: 'Email', from: 'universal@dev.com', to: 'david@packages.com', template: 'templates/email', locals: { name: 'Omar' } })
`
- engine string | EngineInterface Default: local | testengineOptions
Engine to use to send the email, by default if NODE_ENV is development local will be used, if NODE_ENV is test the the test engine will be used.
- Objectrenderer
Any options that the engine constructor accepts
- string | EngineInterface Default: replacerrendererOptions
When specifying template when sending an email Mailing will use the renderer to use a template file.
- ObjecttemplatesLocation
Any options that the renderer constructor accepts
- String Default: ./src
Where the templates for the emails will live.
#### prepare()
Prepares engine and renderer internally.
#### send(sendOptions: Options)
Sends an email using the configured engine.
#### Options
- bcc String | String[]cc
Comma separated list or an array of recipients e-mail addresses that will appear on the Bcc: field.
- String | String[]extra
Comma separated list or an array of recipients e-mail addresses that will appear on the Cc: field.
- Objectfrom
Any extra options the engine may accept.
- Stringhtml
The e-mail address of the sender. All e-mail addresses can be plain 'sender@server.com' or formatted 'Sender Name
- Stringlocale
The HTML version of the message.
- Stringtemplates/email.en
If provided this locale will be appended to the template name. ex locals
- Objectsender
Variables to be passed to the renderer to evaluate on templates.
- Stringsubject
An e-mail address that will appear on the Sender: field.
- Stringtemplate
The subject of the e-mail.
- StringtemplatesLocation
The name of the template without extension relative to the .text
- Stringtext
The plaintext version of the message.
- String | String[]
Comma separated list or an array of recipients e-mail addresses that will appear on the To: field.
The replacer renderer takes templates with the html and txt extensions to fullfil the html and text options of the message.
You can pass locals when sending and this renderer will replace their values when matching {{
`html`
Hi my name is {{ name }}
In the above example you will need to provided the local name to be replaced there in {{ name }}.
The local engine instead of sending the email via internet will just open the email in the explorer.
The test engine is useful to mock to later expect a sending.
You will need to set the mock manually depending on the test framework you are using. For example for jest:
`js
import { TestEngine } from '@universal-packages/mailing'
TestEngine.mock = jest.fn()
`
Mailing will emit events as follow.
`js``
mailing.on('send:start', (event) => console.log(event))
mailing.on('send:end', (event) => console.log(event))
This library is developed in TypeScript and shipped fully typed.
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.
- Code of Conduct
- Contributing Guide