How to send email using sendgrid in Nodejs using dynamicTemplateData?

222 Views Asked by At

This my first time I use sendgrid for mail. I have problem with dynamic data passed from database. Actually HTML content passed in object and display as it is in mail. For more detail check below object.

{
    to: "to Email"
    from: "from email",
    templateId: "Template ID",
    dynamicTemplateData: {
        subject: "Testing Templates",
        name: "Some One",
        city: "Denver",
        week: "August 24 2020",
        job0: {
            header: "Test",
            body: "<table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="
            snip ">We are looking for physically fit individuals to fill general labour requirements by partnering with a truck driver. Your help is needed to load/unload the truck at the various job sites. <b>Skills required: </b> - comfortable with physical exertion and lifting minimum of 50lbs - works well on a team but trusted to work independently - reliable, self-motivated and committed to high standards of quality - able to read and understand work instructions <b>Specific requirements: </b> - in good physical condition - must have own safety footwear - reliable transportation to ensure punctual and consistent attendance If you meet the qualifications listed above, submit your resume in MS Word format via the link below. <i>Previously employed with The Staffing Connection? Please contact our office to confirm your continued availability for these upcoming positions.</i></td> </tr> </tbody> </table>",
            cta: "Apply now",
            url: "My URL"
        },
    }
}

Code

    getJobs().then((jobs) => {
      var mailPayload = {
        to: emails,
        from: "",
        templateId: "",
        dynamicTemplateData: {
          subject: "Testing Templates",
          name: "Some One",
          city: "Denver",
          week: "August 24 2020",
        },
      };
      for (let i = 0; i < jobs.length; i++) {
        mailPayload.dynamicTemplateData["job" + i] = {
          header: jobs[i].job_title,
          body: jobs[i].job_description,
          cta: "Apply now",
          url: jobs[i].company_url,
        };
      }
      mail(mailPayload);
      res.send("Mail sent Successfully").status(200);
    });
  });

Here is the Output

enter image description here

Please help me to solve this issue.

0

There are 0 best solutions below