user9364222
user9364222

Reputation:

Send Email via sendgrid using v3 API

I am trying to send the emails via Sendgrid using v3 API doing so, I want to pass the json data similar to this

{ 
"personalizations": [
    {
      "to": [
        {
          "email": "[email protected]",
          "name": "John Doe"
        }
      ],
      "subject": "Hello, World!"
    }
  ],
  "from": {
    "email": "[email protected]",
    "name": "Sam Smith"
  },
  "reply_to": {
    "email": "[email protected]",
    "name": "Sam Smith"
  }
}

my code :

$email_content = [
                'personalizations' => [
                    'to' => [
                        'email' => '[email protected]',
                        'name' => 'Ashutosh'
                    ],
                    'subject' => 'Test'
                ],
                'from' => [
                    'email' => '[email protected]',
                    'name' => 'Ashu'
                ],
                'reply_to' => [
                    'email' => '[email protected]',
                    'name' => 'AO'
                ],
                'content' => [
                    'type' => 'text/plain',
                    'value' => 'Hello'
                ]
            ];

after json_encode() output is coming in following format:

{
   "personalizations":{
      "to":{
         "email":"[email protected]",
         "name":"Ashutosh"
      },
      "subject":"Test"
   },
   "from":{
      "email":"[email protected]",
      "name":"Ashu"
   },
   "reply_to":{
      "email":"[email protected]",
      "name":"AO"
   },
   "content":{
      "type":"text\/plain",
      "value":"Hello"
   }
}

Any help will be appreciated. I want to use only v3 API for specific reasons

Upvotes: 0

Views: 1189

Answers (2)

bomz
bomz

Reputation: 186

Looking at your json structure it does look different to how they show it in their documentation, notice that "personalizations" and "to" are objects. https://sendgrid.com/docs/API_Reference/api_v3.html, they also stringify the data before sending.

var data = JSON.stringify({
  "personalizations": [
    {
      "to": [
        {
          "email": "[email protected]",
          "name": "John Doe"
        }
      ],
      "subject": "Hello, World!"
    }
  ],
  "from": {
    "email": "[email protected]",
    "name": "Sam Smith"
  },
  "reply_to": {
    "email": "[email protected]",
    "name": "Sam Smith"
  }
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://api.sendgrid.com/v3/mail/send");
xhr.setRequestHeader("authorization", "Bearer <<YOUR_API_KEY>>");
xhr.setRequestHeader("content-type", "application/json");

xhr.send(data);

Hope this helps you.

Upvotes: 2

skparwal
skparwal

Reputation: 1084

You need to add [] - brackets to your 'to' array. Please have a look.

'to' => [
    [ // add this brackets
        'email' => '[email protected]',
        'name' => 'Ashutosh'
    ] // add this brackets 
],

The output would be same as per your requirement.

Upvotes: 1

Related Questions