forked from afj176/generator-mongoose
-
Notifications
You must be signed in to change notification settings - Fork 8
Using Email System in v0.3.0
Abhishek Deb edited this page Mar 22, 2018
·
1 revision
The new revamped Email System can be configured in runtime and used out-of-the-box.
Currently, Node-mailer, email-templates, sendgrid and Mailgun is supported. More Services will be integrated in future and custom hooks will be provided for adding custom Email Provider.
The email system is based on ezmailer. Hence, this requires a configuration file to run with.
Default configuration is given in ./config/global and ./config/development.
- Put in all your API keys and/or service providers (config/development or config/production).
- Require the config file.
var config=require('./config');
- require email from apiObjects in app.js .
var ezmailer = require('./apiObjects/email');
- pass this configuration object for mailService, a default mail provider and default template path.
ezmailer.use(config.mailService,'mailer');
- config.mailService looks like
config.mailService={
templatePath:'./',
mailer:{ //Node-mailer
service:'Gmail',
user:'[Your_Gmail_Id]@gmail.com',
pass:'[Your_Gmail_pass]'
},
sendgrid: '[Your_SendGrid_Key]',
mailgun:{
apiKey:'[Your_Mailgun_key]',
domain:'[Your_website_domain]'
}
};
- 'mailer' option sets the default mail system to be node-mailer ( recommended)
- passing
null
as third argument sets the default emailTemplate Directory to be./emailTemplates/
require the email object anywhere in the api and use sendMail
let ezmailer=require('../apiObjects/email');
let mailOptions={
from:'v@g.com',
to:'abhishekdeb91@gmail.com',
subject:'Sample Basic ezmailer test',
body:{
text:'This is basic ezmailer test. ',
html:'<h1>This is <u>Basic</u> <i>ezmailer</i> test.'
}
};
//Send the mail. sendMaill returns a promise.
ezmailer.sendMail(mailOptions).then(data=>{
console.log('Email has been sent : ',data);
},err=>{
console.log('Email could not be sent : ',err);
});
The sendMail returns a promise (yay!).
let templateMailOptions={
from:'v@g.com',
to:'abh@gmail.com',
subject:'Sample Basic ezmailer test',
data:{
userName:'Abhishek'
}
};
ezmailer.sendMailTemplate('./sample-email-template/html',templateMailOptions).then(data=>{
console.log('Template Email has been sent : ',data);
},err=>{
console.log('Template Email couldn\'t be sent! : ',err);
});
the first argument is the location of html handlebar template.
- Capture the module -
var ezmailer=require('../apiObjects/email');
- Change Default Transporter -
ezmailer.transporter={};
- Change Root Template Directory -
ezmailer.templateDir='./';
- Change View Engine -
ezmailer.templateExtension='hbs'; // or pug, etc
. Hbs is installed by default. You need to install other view engines. - Change Default Mailer System -
ezmailer.defaultSystem='mailer'; //mailer, sendgrid, mailgun