Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SendGrid API with Dynamic Templates with NodeJS

I have a dynamic template in SendGrid, s.t. using Postman with a POST request to https://api.sendgrid.com/v3/mail/send

with the following json:

{
    "personalizations": [{
        "to": [{
            "email": "[email protected]",
            "name": "test"
        }],
        "dynamic_template_data":{
            "name":"John",
            "text": "Someone just added a new post!",
        }
    }],
    "from": {
        "email": "[email protected]",
        "name": "test"
    },
    "reply_to": {
        "email": "[email protected]",
        "name": "test"
    },
    "template_id": "d-c8f201dd360d40bc877da13a1a0b36b1"
}

works to send an email using the template.

However, I can't seem to send an analogous request from Express.

Here is my route that calls SendGrid (end point responds to a cron job):

const express = require('express');
const User = require('../models/User');
const Act = require('../models/Act');
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

let router = express.Router();

router.get('/', async (req, res) => { 
  // getdate 14 days ago
  var ago_date = new Date();
  ago_date.setDate(ago_date.getDate()-0)
  // loop through users
  console.log('crons: getting users');
  const users = await User.query();
  console.log('crons:  number of users: ', users.length);
  for (const user of users) {

console.log(user.username)
const lastAct = await Act
  .query()
  .where('users_id', user.id)
  .orderBy('created_at', 'desc')
  .limit(1);

  const msg = {
    to: '[email protected]',
    from: '[email protected]',
    templateId: 'd-c8f201dd360d40bc877da13a1a0b36b1',

    dynamic_template_data: {
      subject: 'Testing Templates',
      name: 'Some One',
      text: 'Denver',
    },
  };

  console.log('this is the msg 2b sent: ', msg)
  const {
    classes: {
      Mail,
    },
  } = require('@sendgrid/helpers');
  const mail = Mail.create(msg);
  const body = mail.toJSON();
  console.log('this is the body: ', body);
  await sgMail.send(msg); 

  res.json({
    success: true, message: 'ok'
  });   // respond back to request
// };
  };

  res.json({
    success: true, message: 'ok' 
  }); 
});

here is the error trace showing an error:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client:

Full trace below:

userone
sending email re on post
this is the msg 2b sent:  { to: '[email protected]',
  from: '[email protected]',
  templateId: 'd-c8f201dd360d40bc877da13a1a0b36b1',
  dynamic_template_data:
   { subject: 'Testing Templates',
     name: 'Some One',
     text: 'Denver' } }
this is the body:  { from: EmailAddress { email: '[email protected]', name: '' },
  subject: undefined,
  personalizations: [ { to: [Array], dynamic_template_data: [Object] } ],
  template_id: 'd-c8f201dd360d40bc877da13a1a0b36b1' }
usertwo
sending email re on post
this is the msg 2b sent:  { to: '[email protected]',
  from: '[email protected]',
  templateId: 'd-c8f201dd360d40bc877da13a1a0b36b1',
  dynamic_template_data:
   { subject: 'Testing Templates',
     name: 'Some One',
     text: 'Denver' } }
this is the body:  { from: EmailAddress { email: '[email protected]', name: '' },
  subject: undefined,
  personalizations: [ { to: [Array], dynamic_template_data: [Object] } ],
  template_id: 'd-c8f201dd360d40bc877da13a1a0b36b1' }
(node:19822) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (/Users/dariusgoore/development/writerboard/writerboard-express-api/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/Users/dariusgoore/development/writerboard/writerboard-express-api/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/Users/dariusgoore/development/writerboard/writerboard-express-api/node_modules/express/lib/response.js:267:15)
    at router.get (/Users/dariusgoore/development/writerboard/writerboard-express-api/src/routes/crons.js:54:11)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:19822) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
like image 976
user2799827 Avatar asked Oct 15 '22 07:10

user2799827


1 Answers

Apologies for the years late response, we have actually reworke the entire backend and tbh, I have no memory of exactly what we did. Anyway, here is the code of the working route, calling SendGrid:

const express = require('express');
const auth = require('../middlewares/authenticate');
const axios = require('axios');
const User = require('../models/User');
const Post = require('../models/Post');
const Group = require('../models/Group');
const Analytics = require('analytics-node');
const client = new Analytics('write key');

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

let router = express.Router();


//POST new post route 
router.post('/', auth.required, async (req, res, next) => {
  // const users = await User.query();
  await Post.query()
  .insert({
    body: req.body.post.body,
    users_id: req.user.id,
    groups_id: req.body.post.groupId,
    parents_id: req.body.post.parents_id
  })
  console.log('tracking code next')
  let poster = await User.query().findById(req.user.id)
  let group = await Group.query().findById(req.body.post.groupId).eager('user')
  client.track({
    event: 'Post created',
    userId: req.user.id,
    properties: {
      body: req.body.post.body
    }
  });
  console.log('sending email re on post')
  let newPost = req.body
  console.log("new post object is: ", newPost)
  console.log("group users count is: ", group.user.length)
  let recipients = group.user.filter( el => el.id !== poster.id );
  console.log("poster is: ", poster.email)
  // console.log("recipients is: ", recipients)
  console.log("recips length is: ", recipients.length)
  
  // check to see if environment is production before sending email
  console.log('this is node environment: ', process.env.NODE_ENV)
  if (process.env.NODE_ENV === 'production') {
    for (let i = 0; i < recipients.length; i++) {
      const msg = {
        from: "[email protected]",
        template_id: process.env.SENDGRID_NEW_POST,
        asm: {
            groupId: 20914 // new post notification list
        },
        personalizations: [{
            to: { email: recipients[i].email },
            dynamic_template_data: {
                subject: "New Post: " + poster.username + " just posted something",
                recipient_name: recipients[i].username, 
                poster_name: poster.username,
                group_name: group.name,
                post_text: newPost.post.body,
                button_url: process.env.VUE_HOME_URL,
            },
        }],
        
      };
      let sendResult = sgMail.send(msg);
    }
  }
  // console.log(sendResult);

  res.json({
    success: true, message: 'ok' 
  });   // respond back to request
});
...

module.exports = router;
like image 161
user2799827 Avatar answered Oct 19 '22 03:10

user2799827