@universal-packages/mailing v1.4.1
Mailing
Mailing basics with modularity.
Install
npm install @universal-packages/mailingMailing
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.
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' } })Options
enginestring | EngineInterfaceDefault: local | testEngine 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.engineOptionsObjectAny options that the engine constructor acceptsrendererstring | EngineInterfaceDefault: replacerWhen specifying template when sending an email Mailing will use the renderer to use a template file.rendererOptionsObjectAny options that the renderer constructor acceptstemplatesLocationStringDefault: ./srcWhere the templates for the emails will live.
Instance methods
prepare()
Prepares engine and renderer internally.
send(sendOptions: Options)
Sends an email using the configured engine.
Options
bccString | String[]Comma separated list or an array of recipients e-mail addresses that will appear on the Bcc: field.ccString | String[]Comma separated list or an array of recipients e-mail addresses that will appear on the Cc: field.extraObjectAny extra options the engine may accept.fromStringThe e-mail address of the sender. All e-mail addresses can be plain 'sender@server.com' or formatted 'Sender Name sender@server.com'.htmlStringThe HTML version of the message.localeStringIf provided this locale will be appended to the template name. extemplates/email.enlocalsObjectVariables to be passed to the renderer to evaluate on templates.senderStringAn e-mail address that will appear on the Sender: field.subjectStringThe subject of the e-mail.templateStringThe name of the template without extension relative to thetemplatesLocation.textStringThe plaintext version of the message.textString | String[]Comma separated list or an array of recipients e-mail addresses that will appear on the To: field.
ReplacerRenderer
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 {{ <local> }}
<html>
<body>
Hi my name is {{ name }}
</body>
</html>In the above example you will need to provided the local name to be replaced there in {{ name }}.
LocalEngine
The local engine instead of sending the email via internet will just open the email in the explorer.
TestEngine
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:
import { TestEngine } from '@universal-packages/mailing'
TestEngine.mock = jest.fn()Events
Mailing will emit events as follow.
mailing.on('send:start', (event) => console.log(event))
mailing.on('send:end', (event) => console.log(event))Typescript
This library is developed in TypeScript and shipped fully typed.
Contributing
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.
License
11 months ago
11 months ago
11 months ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago