Hello guys,
In this blog, we will discuss how to send email in sails.js using sails-email-hook and SendGrid service.
first of all, install sails-hook-email in your project directory-
$ npm install sails-hook-email
and then create an account on SendGrid.
SendGrid provides email delivery service for email sending we use SendGrid email service after creating an account on SendGrid it will give username and password for authentication account for using email service.
Create a file in named email.js inside project directory config/ folder.
1 2 3 4 5 6 7 8 9 10 11 |
module.exports.email = { service: "SendGrid", auth: { user:"username", pass:"password" }, templateDir: "api/emailTemplates", from: "email_address", testMode:false, ssl: false } |
Create a file named EmailService inside service folder in a project directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
module.exports={ sendWelcomeMail : function(obj) { sails.hooks.email.send( "welcomeEmail", { Name:'Hi Dear', accessToken:obj.accessToken }, { to: obj.email, subject: "Welcome Email" }, function(err) { if(err) { console.log('Email not send sucessfully-',err); } else { console.log('Email send sucessfully'); } } ) } }; |
After that create email template which is view on email inside api/emailTemplates/welcomeEmail.
call email service create an action inside api/controllers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
signup: async function (req, res) { if (req.method === 'POST') { if(req.body.email && req.body.password) { console.log("Re Body : ", req.body); let existingUser = await User.findOne({email: req.body.email}); console.log("Existing User : ", existingUser); if (existingUser) { console.log(" If Existing User : "); return res.json({message: 'User already exist with this email. Please try to login'}); } else if (!existingUser) { let role = req.body.role; let userData = { email: req.body.email, password: req.body.password, role: await Role.findOne({authority: role}), accessToken: uuid.v4() }; Role.findOne({authority: req.body.role}).exec(function (err, role) { if (err) { console.log('error-', error); return res.json({error: err}); } else if (role == undefined) { return res.json({message: 'role not defined'}); } else { if(req.body.role==='ROLE_PLAYER') { User.create(userData).exec(function (err,user) { if(err) { console.log('error-',err); res.json({error:err}); }else if(user) { EmailService.sendWelcomeMail(user); return res.json({message: 'User Created Successfully!'}); }else { return res.json({message:'User not create successfully!'}); } }) }else { return res.json({message:'User not create with this role'}); } } }); } } else { return res.json({message:'Email OR Password Missing! '}); } } } |
In the above code EmailService.sendWelcomeMail(user) function user argument contain email and other data.
On the call, this function sends function is called using sails-hook-email which is send an email template to create inside api/emailTeamplates/welcomeEmail folder to the email.
Recent Comments