如何在 PHP 中通过 guzzle HTTP 发送动态模板邮件
How to send dynamic template mail via guzzle HTTP in PHP
我正在尝试在 PHP 中使用 Guzzle HTTP 通过 SendGrid 中的动态模板发送电子邮件。
但是我无法发送邮件。因为我没有任何原因只得到如下错误。
Type: GuzzleHttp\Exception\ClientException
Message: Client error: `POST https://api.sendgrid.com/v3/mail/send` resulted in a `400 Bad Request` response: {"errors":[{"message":"Bad Request","field":null,"help":null}]}
我的PHP示例代码:
require __DIR__.'../../vendor/autoload.php';
$CLIENT = new GuzzleHttp\Client();
$response = $CLIENT->request('POST', 'https://api.sendgrid.com/v3/mail/send', [
"headers" => [
"Authorization" => "Bearer my-api-key",
"Content-Type" => "application/json"
],
'data' => '{
"from": {
"email": "admin@example.com"
},
"personalizations": [{
"to": [{
"email": "me@gmail.com"
}],
"dynamic_template_data": {
"email_data": [{
"id": "2",
"title": "Artificial Intelligence in Health Care",
"image": "https://example.com//uploads/3663581583995181_0724.jpg",
"description": "Immediate application of AI in the Health Care domains."
}, {
"id": "199",
"title": "Aesthetics Skill Discussion 3 by Jranand",
"image": "",
"description": "Aesthetics Skill Discussion 3 by Jranand"
}]
}
}],
"template_id": "my-template-id"
}',
]);
echo $response->getStatusCode();
我能够使用相同的 dynamic_template_data 通过 SendGrid 中的动态模板测试方法发送电子邮件。但是用 Guzzle HTTP 试试这个。我找不到错误的原因。
能够在测试中发送带有动态 JSON 数据的电子邮件。
谁能帮我解决这个问题?
提前致谢。
json中没有data请求选项,你需要更改代码或者使用json 从 guzzle 请求选项(将需要较少的努力)或者您可以直接纠正您的 body。
try{
$CLIENT = new GuzzleHttp\Client();
$data = [
"from" => [
"email" => "admin@example.com"
],
"personalizations" => [
[
"to" => [
[
"email" => "me@gmail.com"
]
],
"dynamic_template_data" => [
"email_data" => [
[
"id" => "2",
"title" => "Artificial Intelligence in Health Care",
"image" => "https://example.com//uploads/3663581583995181_0724.jpg",
"description"=> "Immediate application of AI in the Health Care domains."
],
[
"id" => "199",
"title" => "Aesthetics Skill Discussion 3 by Jranand",
"image" => "",
"description" => "Aesthetics Skill Discussion 3 by Jranand"
]
]
]
]
],
"template_id" => "my-template-id"
];
$response = $CLIENT->request('POST', 'https://api.sendgrid.com/v3/mail/send', [
"headers" => [
"Authorization" => "Bearer my-api-key",
"Content-Type" => "application/json"
],
'json' => $data,
]);
if ($guzzleResponse->getStatusCode() == 200) {
$response = json_decode($guzzleResponse->getBody(),true);
//perform your action with $response
}
}
catch(\GuzzleHttp\Exception\RequestException $e){
// you can catch here 400 response errors and 500 response errors and log those errors
}catch(Exception $e){
//other errors
}
我正在尝试在 PHP 中使用 Guzzle HTTP 通过 SendGrid 中的动态模板发送电子邮件。
但是我无法发送邮件。因为我没有任何原因只得到如下错误。
Type: GuzzleHttp\Exception\ClientException
Message: Client error: `POST https://api.sendgrid.com/v3/mail/send` resulted in a `400 Bad Request` response: {"errors":[{"message":"Bad Request","field":null,"help":null}]}
我的PHP示例代码:
require __DIR__.'../../vendor/autoload.php';
$CLIENT = new GuzzleHttp\Client();
$response = $CLIENT->request('POST', 'https://api.sendgrid.com/v3/mail/send', [
"headers" => [
"Authorization" => "Bearer my-api-key",
"Content-Type" => "application/json"
],
'data' => '{
"from": {
"email": "admin@example.com"
},
"personalizations": [{
"to": [{
"email": "me@gmail.com"
}],
"dynamic_template_data": {
"email_data": [{
"id": "2",
"title": "Artificial Intelligence in Health Care",
"image": "https://example.com//uploads/3663581583995181_0724.jpg",
"description": "Immediate application of AI in the Health Care domains."
}, {
"id": "199",
"title": "Aesthetics Skill Discussion 3 by Jranand",
"image": "",
"description": "Aesthetics Skill Discussion 3 by Jranand"
}]
}
}],
"template_id": "my-template-id"
}',
]);
echo $response->getStatusCode();
我能够使用相同的 dynamic_template_data 通过 SendGrid 中的动态模板测试方法发送电子邮件。但是用 Guzzle HTTP 试试这个。我找不到错误的原因。
能够在测试中发送带有动态 JSON 数据的电子邮件。
谁能帮我解决这个问题?
提前致谢。
json中没有data请求选项,你需要更改代码或者使用json 从 guzzle 请求选项(将需要较少的努力)或者您可以直接纠正您的 body。
try{
$CLIENT = new GuzzleHttp\Client();
$data = [
"from" => [
"email" => "admin@example.com"
],
"personalizations" => [
[
"to" => [
[
"email" => "me@gmail.com"
]
],
"dynamic_template_data" => [
"email_data" => [
[
"id" => "2",
"title" => "Artificial Intelligence in Health Care",
"image" => "https://example.com//uploads/3663581583995181_0724.jpg",
"description"=> "Immediate application of AI in the Health Care domains."
],
[
"id" => "199",
"title" => "Aesthetics Skill Discussion 3 by Jranand",
"image" => "",
"description" => "Aesthetics Skill Discussion 3 by Jranand"
]
]
]
]
],
"template_id" => "my-template-id"
];
$response = $CLIENT->request('POST', 'https://api.sendgrid.com/v3/mail/send', [
"headers" => [
"Authorization" => "Bearer my-api-key",
"Content-Type" => "application/json"
],
'json' => $data,
]);
if ($guzzleResponse->getStatusCode() == 200) {
$response = json_decode($guzzleResponse->getBody(),true);
//perform your action with $response
}
}
catch(\GuzzleHttp\Exception\RequestException $e){
// you can catch here 400 response errors and 500 response errors and log those errors
}catch(Exception $e){
//other errors
}