A syntax fluid mail sender library that uses SES and NodeMailer.
Following environment variables must be defined:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
With npm:
npm i fluid-ses-core fluid-ses-templating
With yarn:
yarn add fluid-ses-core fluid-ses-templating
yarn && yarn run build:all
yarn run test:all
const fluidSes = new FluidSes({ region: 'eu-west-1' });
await fluidSes.sourceName('Fluid mailer')
.sourceMail('[email protected]')
.addressees(['[email protected]','[email protected]'])
.subject('Urgent matter')
.template('My incredible message')
.sendMail();
In order to avoid specifying sender identity each time, you can give default value for those
const fluidSes = new FluidSes({
region: 'eu-west-1',
defaultSourceName: 'System sender',
defaultSourceMail: '[email protected]'
});
Fluid SES allows you to filter addressees based on a custom function. If addressee list ends up empty after the call of the mailTrap method, it ends up not sending a mail at all. Here is the signature :
mailTrap: (addressees: Array<string | Address>) => Promise<Array<string | Address>>;
Example:
const myMailTrap = async (addressees: Array<string | Address>) => {
return process.env.env === 'prod' ? addressees : [];
}
const fluidSes = new FluidSes({
region: 'eu-west-1',
mailTrap: myMailTrap
});
If you want to use another template engine, you can define new ones this way
class MyCustomTemplateEngine extends ITemplateEngine {
getComputedTemplate (options: CustomTemplatingOptions): Promise<string> {
// Process and then returns filled template as a promise
};
}
const fluidSes = new FluidSes({
region: 'eu-west-1',
templateEngine: MyCustomTemplateEngine
});
const defaultTemplate = 'Hello {{ name }}, ' +
'your {{ article }} with number {{ command }} has been shipped.';
const myTemplate: ITemplateEngineOptions = {
template: defaultTemplate,
templateMapping: {
name: 'Joe',
article: 'Computer',
command: '21673',
},
};
await fluidSes.sourceName('Fluid mailer')
.sourceMail('[email protected]')
.addressees(['[email protected]','[email protected]'])
.subject('Urgent matter')
.template(myTemplate)
.sendMail();
await fluidSes.sourceName('Fluid mailer')
.sourceMail('[email protected]')
.addressees(['[email protected]','[email protected]'])
.subject('Urgent matter')
.useBcc(true) // default is false
.template('My incredible message')
.sendMail();
This will use only the first addressee of the array as 'to' material and the others will be added as bcc.
This allows you to add attachments to your mails.
const attachments: Attachment[] = [{ filename: 'something' }];
await fluidSes.sourceName('Fluid mailer')
.sourceMail('[email protected]')
.addressees(['[email protected]','[email protected]'])
.subject('Urgent matter')
.attachments(attachments)
.template('My incredible message')
.sendMail();
If you are willing to use Nodemailer options that aren't already supported by Fluid SES you can still use them this way :
const myNodeMailerOptions: Mail.Options = {
encoding: 'utf-8',
};
await fluidSes.sourceName('Fluid mailer')
.sourceMail('[email protected]')
.addressees(['[email protected]','[email protected]'])
.subject('Urgent matter')
.additionalOptions(myNodeMailerOptions) // Won't override options comming natively from Fluid SES
.template('My incredible message')
.sendMail();
await fluidSes.sourceName('Fluid mailer')
.sourceMail('[email protected]')
.addressees(['[email protected]','[email protected]'])
.subject('Urgent matter')
.additionalOptions(myNodeMailerOptions, true) // Will override options comming natively from Fluid SES
.template('My incredible message')
.sendMail();