Salesforce | Send email through SendGrid using REST Api

1.10.2019
|
0 Comments
|
|

How to send an email through SendGrid using REST API in salesforce

For sending the email through Sendgrid, we must have the Sendgrid Account’s Key and this Key will use in the Rest API to send the email.

Some parameters of the Rest API are:

API: The Sendgrid’s API for sending email is – https://api.sendgrid.com/v3/mail/send

Key: The key to the Sendgrid Account is like XX.XXXXXXXXXXXXXXXX.XXXXXXXXXXXXXXXXXX

Here is the example of JSON Request body of the email:

{
  "personalizations": [
    {
      "to": [
        {
          "email": "test@gmail.com"
        }
      ],
      "subject": "Hello, World!"
    }
  ],
  "from": {
    "email": "sender@gmail.com"
  },
  "content": [
    {
      "type": "text/plain",
      "value": "Hello, This is the email body!"
    }
  ]
}

Brief description of Request body parameters:

1. Personalizations: This is the array of objects. We can add only 1000 personalizations per API request.

2. To, cc and bcc: They include the recipients of the email. The total number of recipients must be less than 1000. This includes all recipients defined within the to, cc, and bcc parameters, across each object of the Personalizations array.

3. From: It includes the sender’s email address.

4. Content: It includes the type and body of the email.

Here is the code of Sendgrid Rest API in Salesforce:

        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(sendGridURL); // Send email API - https://api.sendgrid.com/v3/mail/send
        request.setMethod('POST');
        request.setHeader('Authorization', 'Bearer ' + key); // The key of the Sendgrid Account
        request.setHeader('Content-Type', 'application/json');
        request.setBody(jsonBody);
        HttpResponse response = new HttpResponse();
        response = http.send(request);

Leave a Reply

Your email address will not be published. Required fields are marked *

Share on:
Share this...
Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Website security
Contact Us