4.0.7-1 • Published 2 years ago
coins-mail v4.0.7-1
HTML and text email templates for COINS.
Example
Create Mail
The createMail is coins-mail’s chief export. It expects a configured Bookshelf.js model:
const knex = require('knex')({
/* DB config ... */
});
const bookshelf = require('bookshelf')(knex);
const createMail = require('coins-mail').createMail;
const MyMailModel = bookshelf.Model.extend({
tableName: 'my_table_name',
});
createMail(MyMailModel, options); // returns PromisecreateMail returns a Promise. In the case of a single mail option, this is the result of calling save on a Bookshelf.js model. In the case of a collection of mail options, this is the result of invoking save on the Bookshelf collection.
Options:
options can be either a mail options object or a collection of these objects:
fromLabel(string): Added to thefrom_labelcolumn. This is typically the name of application sending the email.recipients(string or array of strings): Single recipient email address or a collection of email addresses.replyTo(string): Email address to use in the email’s 'reply to' field.subject(string): Email’s subject field.templateLocals(object): Hash to pass togetTemplate. The keys/values depend on the email template in use (seetemplateName).sendTime(string, defaults toDate.now()): Time to send the email.templateName(string, defaults to “default“): Name of email template to use.
Single email
createMail(MyMailModel, {
fromLabel: 'Test Application',,
recipients: [
'recipient-1@mrn.org',
'recipeint-2@mrn.org',
'recipient-3@mrn.org',
// ...
],
replyTo: 'reply-address@mrn.org',
sendTime: Date.now() + 24 * 60 * 60 * 1000,
subject: 'Test Subject',
templateLocals: {
html: {
myHtmlTemplateVar: 'value',
},
text: {
myTextTemplateVar: 'value',
},
},
templateName: 'my-template',
})
.then(savedModel => console.log(savedModel))
.catch(error => console.error(error));Multiple emails
createMail(MyMailModel, [{
fromLabel: 'Test Application 1',
recipients: 'recipient-1@mrn.org'
replyTo: 'reply-address@mrn.org',
subject: 'Test Subject 1',
templateLocals: // ...
}, {
fromLabel: 'Test Application 2',
recipients: 'recipient-2@mrn.org'
replyTo: 'reply-address@mrn.org',
subject: 'Test Subject 2',
templateLocals: // ...
}, {
fromLabel: 'Test Application 3',
recipients: 'recipient-3@mrn.org'
replyTo: 'reply-address@mrn.org',
subject: 'Test Subject 3',
templateLocals: // ...
}])
.then(savedModels => {
savedModels.forEach(savedModel => console.log(savedModel));
})
.catch(error => console.error(error));Template
const getTemplate = require('coins-mail').getTemplate;
getTemplate({
html: {
myHtmlTemplateVar: '<h1>Hi!</h1> <p>Insert this html…</p>',
},
text: {
myTextTemplateVar: 'Alternative!\n\nFor the text version…',
},
}, (error, result) => {
if (error) {
console.error(error);
} else {
console.log('HTML:', result.html);
console.log('Text:', result.text);
}
});getTemplate also returns a Promise:
getTemplate(myLocals)
.then(result => {
console.log('HTML:', result.html);
console.log('Text:', result.text);
})
.catch(error => console.error(error));License
MIT. See LICENSE for details.