简体   繁体   English

使用v3 API通过sendgrid发送电子邮件

[英]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 我正在尝试使用v3 API通过Sendgrid发送电子邮件,我想传递与此类似的json数据

{ 
"personalizations": [
    {
      "to": [
        {
          "email": "john.doe@example.com",
          "name": "John Doe"
        }
      ],
      "subject": "Hello, World!"
    }
  ],
  "from": {
    "email": "sam.smith@example.com",
    "name": "Sam Smith"
  },
  "reply_to": {
    "email": "sam.smith@example.com",
    "name": "Sam Smith"
  }
}

my code : 我的代码:

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

after json_encode() output is coming in following format: 在json_encode()输出采用以下格式后:

{
   "personalizations":{
      "to":{
         "email":"ashuomble5@gmail.com",
         "name":"Ashutosh"
      },
      "subject":"Test"
   },
   "from":{
      "email":"ashuomble5@gmail.com",
      "name":"Ashu"
   },
   "reply_to":{
      "email":"ashuomble5@gmail.com",
      "name":"AO"
   },
   "content":{
      "type":"text\/plain",
      "value":"Hello"
   }
}

Any help will be appreciated. 任何帮助将不胜感激。 I want to use only v3 API for specific reasons 出于特定原因,我只想使用v3 API

Looking at your json structure it does look different to how they show it in their documentation, notice that "personalizations" and "to" are objects. 查看您的json结构,它的确与他们在文档中显示的样子不同,请注意,“个性化”和“至”是对象。 https://sendgrid.com/docs/API_Reference/api_v3.html , they also stringify the data before sending. https://sendgrid.com/docs/API_Reference/api_v3.html ,它们还会在发送之前对数据进行字符串化处理。

var data = JSON.stringify({
  "personalizations": [
    {
      "to": [
        {
          "email": "john.doe@example.com",
          "name": "John Doe"
        }
      ],
      "subject": "Hello, World!"
    }
  ],
  "from": {
    "email": "sam.smith@example.com",
    "name": "Sam Smith"
  },
  "reply_to": {
    "email": "sam.smith@example.com",
    "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. 希望这对您有所帮助。

You need to add [] - brackets to your 'to' array. 您需要在[to]数组中添加[]-括号。 Please have a look. 请看一看。

'to' => [
    [ // add this brackets
        'email' => 'ashuomble5@gmail.com',
        'name' => 'Ashutosh'
    ] // add this brackets 
],

The output would be same as per your requirement. 输出将与您的要求相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM