Sendgrid: Send Bulk Emails

Sendgrid: Send Bulk Emails
Send a mail from SendGrid.
2019-11-08 (C) Questetra, Inc. (MIT License)
Configs
  • C1: API Key *
  • C2: Source Mail Address *
  • C3: Source Name
  • C4: Template ID *
  • C5: Table Item with destination and dynamic template data *
Script
main();
function main(){
  const tempId = configs.get("Template");
  const fromAddress = configs.get("FromAddress");
  const dynamicTemp = engine.findDataByNumber(configs.get("DynamicTemp"));
  const dyTempData = engine.findDataDefinitionByNumber(configs.get("DynamicTemp"));
  const fromName = configs.get("FromName");
  let subData;
  const json = {
    "personalizations": [
      
    ],
    "from": {},
  };
  json["template_id"] = tempId;
  json["from"]["email"] = fromAddress;
  if(fromName !== "" && fromName !== null){
    json["from"]["name"] = fromName;
  }
  if(dynamicTemp === null){
    throw "the table data is empty.";
  }
  const tableSize = dynamicTemp.size();
  const destinationLimit = 200;
  const subdataLimit = 50;
  if(tableSize > destinationLimit){
    throw "number of destination mail addresses can't be over " + destinationLimit + ".";
  }
  if(dynamicTemp.getRow(0).size() > subdataLimit){
      throw "number of subdata can't be over " + subdataLimit + ".";
    }
  for(i=0;i<tableSize;i++){
    json["personalizations"][i] = {
        "to": [{}],
        "dynamic_template_data": {},
      };
    if(dynamicTemp.get(i,0) === "" || dynamicTemp.get(i,0) === null){
      throw "Destination mail address is empty.";
    }
    json["personalizations"][i]["to"][0]["email"] = dynamicTemp.get(i,0);
    if(dynamicTemp.getRow(0).size() > 1){
      if(dynamicTemp.get(i,1) !== "" && dynamicTemp.get(i,1) !== null){
        if(dynamicTemp.get(i,1).search(/[;\,]/) !== -1){
          throw "invalid character is included in the destination name";
        }
        json["personalizations"][i]["to"][0]["name"] = dynamicTemp.get(i,1)
      }
      for(j=2;j<dynamicTemp.getRow(0).size();j++){
        subData = dyTempData.getSubDataDefinitions()[j].getName();
        if(subData !== "" && subData !== null){
          json["personalizations"][i]["dynamic_template_data"][subData] = dynamicTemp.get(i,j);
        }
      }
    }
  }
 const response = httpClient.begin()
    .bearer(configs.get("Key"))
    .body(JSON.stringify(json),"application/json; charset=UTF-8")
    .post("https://api.sendgrid.com/v3/mail/send");

  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();
  if (status >= 300) {
    const error = "Failed to send \n status:" + status + "\n" + responseTxt
    throw error;
  }
  engine.log(status);
  engine.log(responseTxt);
}

Download

Capture

Notes

  1. C5 is a Table data Item. You can use one line per destination.
  2. Each destination address must be designated in the first column, the display name in the second column. And the embed string used in the SendGrid Template must be designated in the third and subsequent columns.
  3. All subsequent sub-data names must be the same as the embed string (e.g. {{Text}} → “Text”) and the string is replaced with the value of the subdata item.
  4. The first and second column can be any name.
%d bloggers like this: