Sendgrid의 트랜잭션 템플릿을 테스트하려고합니다. 값이 하드 코딩 된 경우 메일을 보낼 수 있었지만 변수를 제공 할 때는 메일을 보낼 수 없었습니다.루프 된 값이 Sendgrid 대체에 대해 작동하지 않지만 동일한 하드 코드 된 값을 사용합니다.
내 헤더 : 살펴 보도록하자
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer my_actual_api_key',
);
내 $ 데이터 배열 :
$resetLink = 'na';
$reset = 'Please, click here to activate your account';
$user = 'MyName';
$data = ['reset'=>$reset, 'user'=>$user, 'resetLink'=>$resetLink];
$ 잠수정을 수행하여 생성됩니다
if($data){
foreach($data as $k=>$v){
$subs.= '"{{'.$k.'}}":"'.$v.'"';
if($last_element != $v){
$subs .=",";
}
}
}
출력한다 :
"{{reset}}": "Please, click here to activate your account.",
"{{user}}": "MyName",
"{{resetLink}}": "na"
내가 원하는 것! 내가 할 경우
그러나 : 내가 할 경우,
{"errors":[{"message":"Bad Request","field":null,"help":null}]}
:하지만
$json = '{
"personalizations": [{
"to": [{
"email": "'.$to.'",
"name": "Example 01"
}],
"subject": "'.$subject.'",
"substitutions": {
'.$subs.'
}
}],
"from": {
"email": "'.$from.'",
"name": "'.$from.'"
},
"reply_to": {
"email": "'.$from.'",
"name": "'.$from.'"
},
"template_id": "My_actual_temnplate_id"
}';
내가 다시 얻을
$json = '{
"personalizations": [{
"to": [{
"email": "'.$to.'",
"name": "Example 01"
}],
"subject": "'.$subject.'",
"substitutions": {
"{{reset}}": "Please, click here to activate your account.",
"{{user}}": "MyName",
"{{resetLink}}": "na"
}
}],
"from": {
"email": "'.$from.'",
"name": "'.$from.'"
},
"reply_to": {
"email": "'.$from.'",
"name": "'.$from.'"
},
"template_id": "1959ba19-3bc7-455b-a577-7a340f4c5fd9"
}';
내가 밖으로 컬 단지 내 메일을 보낼 수 있습니다 다음을 사용하여 오류없이 잘 처리 할 수 있습니다.
은을 heres 컬, 표준 물건 :$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sendgrid.com/v3/mail/send");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
는 또한 배열로이 JSON을 한 후 위와 같이, 중() 같은 오류가 아닌 메시지가 운로 json_encode를 사용하지 않고 노력했다. 이것은 sendgrid 사이트에서 제공되는 json이며 fyi입니다.
나는 당신이 발견 한 대부분의 답을 설명하는 모든 것을 문자열로 가지고있다. 누군가 이런 일이 발생하는 이유에 대해 밝힐 수 있습니까? 미리 감사드립니다!