Urooj
Urooj

Reputation: 354

How to send personalized emails to multiple addresses using sendgrid in NodeJS

I have an array of email addresses and an array of passwords. I want to send each password to its corresponding email address(email at same index) in the body of email. But I don't want to use a loop. Is it possible to use substitution in the email body and then have sendgrid pick a value from the password array for each email address. I know I can construct a personalizations object like so :

personalizations: {to: [{email: "email1"}], substitutions: {"-pwd-": "pwd1"}}

and then use -pwd- in the body of email. But to construct this object I again have to use a loop which I don't want.

Upvotes: 1

Views: 844

Answers (1)

Adam Urban
Adam Urban

Reputation: 46

Haven't tried it out, but according to the SendGrid Docs it should be able.

The given example in the docs (for SendGrid API v3) looks the following way:

{
  "personalizations": [
    {
      "to": [
        {
          "email": "john@domain.com",
          "name": "John"
        }
      ],
      "subject": "Example 01",
      "substitutions": {
        "-name-": "John"
      }
    },
    {
      "to": [
        {
          "email": "jane@domain.com",
          "name": "Jane"
        }
      ],
      "subject": "Example 02",
      "substitutions": {
        "-name-": "Jane"
      }
    },
    {
      "to": [
        {
          "email": "matt@domain.com",
          "name": "Matt"
        }
      ],
      "subject": "Example 03",
      "substitutions": {
        "-name-": "Matt"
      }
    }
  ],
  "from": {
    "email": "sender@senddomain.com",
    "name": "Sender"
  },
  "reply_to": {
    "email": "sender@senddomain.com",
    "name": "Sender"
  },
  "subject": "Example",
  "content": [
    {
      "type": "text/plain",
      "value": "Hello -name-,"
    },
    {
      "type": "text/html",
      "value": "Hello -name-,"
    }
  ]
}

By using Dynamic Transactional Templates it should work the same way with handlebars.

Upvotes: 1

Related Questions