Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用v3 API通过sendgrid发送电子邮件_Php_Json_Email_Sendgrid_Sendgrid Api V3 - Fatal编程技术网

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

Php 使用v3 API通过sendgrid发送电子邮件,php,json,email,sendgrid,sendgrid-api-v3,Php,Json,Email,Sendgrid,Sendgrid Api V3,我正在尝试使用V3API通过Sendgrid发送电子邮件,我希望传递类似于此的json数据 { "personalizations": [ { "to": [ { "email": "john.doe@example.com", "name": "John Doe" } ], "subject": "Hello, World!" } ], "from": {

我正在尝试使用V3API通过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"
  }
}
我的代码:

$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'
                ]
            ];
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"
   }
}

任何帮助都将不胜感激。出于特定原因,我只想使用v3api

您需要在“to”数组中添加[]-括号。请看一看

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

输出将与您的要求相同。

您需要在“to”数组中添加[]-括号。请看一看

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

输出将与您的要求相同。

查看您的json结构,它看起来确实不同于它们在文档中的显示方式,请注意,“personalizations”和“to”是对象,它们还可以在发送之前对数据进行字符串化

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);
var data=JSON.stringify({
“个性化”:[
{
“致”:[
{
“电子邮件”:“约翰。doe@example.com",
“姓名”:“约翰·多伊”
}
],
“主题”:“你好,世界!”
}
],
“发件人”:{
“电子邮件”:“山姆。smith@example.com",
“姓名”:“山姆·史密斯”
},
“答复”:{
“电子邮件”:“山姆。smith@example.com",
“姓名”:“山姆·史密斯”
}
});
var xhr=new XMLHttpRequest();
xhr.withCredentials=true;
xhr.addEventListener(“readystatechange”,函数(){
if(this.readyState==this.DONE){
console.log(this.responseText);
}
});
xhr.open(“POST”https://api.sendgrid.com/v3/mail/send");
xhr.setRequestHeader(“授权”、“持有人”);
setRequestHeader(“内容类型”、“应用程序/json”);
发送(数据);

希望这对你有帮助

看看你的json结构,它看起来确实与他们在文档中的显示方式不同,注意“personalizations”和“to”是对象,它们还可以在发送之前对数据进行字符串化

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);
var data=JSON.stringify({
“个性化”:[
{
“致”:[
{
“电子邮件”:“约翰。doe@example.com",
“姓名”:“约翰·多伊”
}
],
“主题”:“你好,世界!”
}
],
“发件人”:{
“电子邮件”:“山姆。smith@example.com",
“姓名”:“山姆·史密斯”
},
“答复”:{
“电子邮件”:“山姆。smith@example.com",
“姓名”:“山姆·史密斯”
}
});
var xhr=new XMLHttpRequest();
xhr.withCredentials=true;
xhr.addEventListener(“readystatechange”,函数(){
if(this.readyState==this.DONE){
console.log(this.responseText);
}
});
xhr.open(“POST”https://api.sendgrid.com/v3/mail/send");
xhr.setRequestHeader(“授权”、“持有人”);
setRequestHeader(“内容类型”、“应用程序/json”);
发送(数据);
希望这对你有帮助