├── README.md └── email.js /README.md: -------------------------------------------------------------------------------- 1 | # Send-Emails-from-Node.js-App-using-Sendgrid-Dynamic-Template 2 | 3 | ## this is the sendgrid module to send emails 4 | ``` 5 | const sgMail = require('@sendgrid/mail') 6 | 7 | const sgMailApiKey = 'your-api-key' 8 | 9 | sgMail.setApiKey(sgMailApiKey) 10 | ``` 11 | ## Method to send normal email - sendgrid 12 | ``` 13 | module.exports.sendEmail = (email, password) => { 14 | 15 | console.log(email +" : "+password) 16 | sgMail.send({ 17 | to: email, 18 | from: 'fixthatdevice@gmail.com', 19 | subject: 'Fix That Device Password Reset', 20 | text: `Hello.
Welocome to Fix That Device.
Your new password is: ${password} `, 21 | html: `

Hello.
Welocome to Fix That Device.
Your new password is: ${password}

` 22 | 23 | }).then(() => {}, error => { 24 | console.error(error); 25 | 26 | if (error.response) { 27 | console.error(error.response.body) 28 | } 29 | }); 30 | 31 | } 32 | ``` 33 | 34 | ## Method to send dynamic template email - sendgrid 35 | ``` 36 | module.exports.sendTemplate = (to,from, templateId, dynamic_template_data) => { 37 | const msg = { 38 | to, 39 | from: { name: 'Fix That Device', email: from }, 40 | templateId, 41 | dynamic_template_data 42 | }; 43 | console.log(msg) 44 | sgMail.send(msg) 45 | .then((response) => { 46 | console.log('mail-sent-successfully', {templateId, dynamic_template_data }); 47 | console.log('response', response); 48 | /* assume success */ 49 | 50 | }) 51 | .catch((error) => { 52 | /* log friendly error */ 53 | console.error('send-grid-error: ', error.toString()); 54 | }); 55 | }; 56 | ``` 57 | -------------------------------------------------------------------------------- /email.js: -------------------------------------------------------------------------------- 1 | const sgMail = require('@sendgrid/mail') 2 | 3 | const sgMailApiKey = 'your-api-key' 4 | 5 | sgMail.setApiKey(sgMailApiKey) 6 | 7 | 8 | 9 | module.exports.sendEmail = (email, password) => { 10 | 11 | console.log(email +" : "+password) 12 | sgMail.send({ 13 | to: email, 14 | from: 'fixthatdevice@gmail.com', 15 | subject: 'Fix That Device Password Reset', 16 | text: `Hello.
Welocome to Fix That Device.
Your new password is: ${password} `, 17 | html: `

Hello.
Welocome to Fix That Device.
Your new password is: ${password}

` 18 | 19 | }).then(() => {}, error => { 20 | console.error(error); 21 | 22 | if (error.response) { 23 | console.error(error.response.body) 24 | } 25 | }); 26 | 27 | } 28 | 29 | 30 | module.exports.sendTemplate = (to,from, templateId, dynamic_template_data) => { 31 | const msg = { 32 | to, 33 | from: { name: 'Fix That Device', email: from }, 34 | templateId, 35 | dynamic_template_data 36 | }; 37 | console.log(msg) 38 | sgMail.send(msg) 39 | .then((response) => { 40 | console.log('mail-sent-successfully', {templateId, dynamic_template_data }); 41 | console.log('response', response); 42 | /* assume success */ 43 | 44 | }) 45 | .catch((error) => { 46 | /* log friendly error */ 47 | console.error('send-grid-error: ', error.toString()); 48 | }); 49 | }; 50 | --------------------------------------------------------------------------------