1

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.

Urooj
  • 151
  • 7

1 Answers1

1

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.

Adam Urban
  • 46
  • 4
  • This is correct, but I would recommend using handlebars and dynamic transactional templates as you have more control over simple substitutions like this. – philnash Jun 20 '22 at 22:32
  • I am not able to understand your answer. I've already mentioned in my question that i know to send personalized emails i can use the personalizations object. But to construct it using my emails and passwords array, i still have to run a loop, which i don't want.Is there any way to make this work directly with the arrays? – Urooj Jun 29 '22 at 04:51