Outlook Rest Api发送邮件请求返回状态400 [英] Outlook Rest Api sending Mail Request returned status 400
问题描述
我尝试从Outlook Rest Api和Curl的签名Outlook帐户发送电子邮件,然后我收到此错误
请求返回状态400
这是我发送邮件的代码
private static $ outlookApiUrl =https://outlook.office.com/api/v2.0;
public static function sendMail($ access_token,$ user_email,$ subject,$ Content,$ email){
$ arr = array(
Message= > array(
'Subject'=> $ subject,
Body=> array(
Content-Type=>HTML,
Content=> $ Content,
),
ToRecipients=> array(
array(
EmailAddress=& => $ email,
)
),
),
));
$ json = json_encode($ arr,true);
$ getMessagesUrl = self :: $ outlookApiUrl。/ me / sendmail;
return self :: makeApiCall($ access_token,$ user_email,POST,$ getMessageUrl,$ json);
}
这是CURL的代码
public static function makeApiCall($ access_token,$ user_email,$ method,$ url,$ payload = NULL){
//生成始终发送的标题列表。
$ headers = array(
User-Agent:php-tutorial / 1.0,
Authorization:Bearer。$ access_token,
Accept:application / json
client-request-id:.self :: makeGuid(),
return-client-request-id:true,
X-AnchorMailbox:。$ user_email
);
$ curl = curl_init($ url);
switch(strtoupper($ method)){
casePOST:
error_log(Doing POST);
$ headers [] =Content-Type:application / json;
curl_setopt($ curl,CURLOPT_POST,true);
curl_setopt($ curl,CURLOPT_POSTFIELDS,$ payload);
break;
默认值:
error_log(INVALID METHOD:。$ method);
exit;
}
curl_setopt($ curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ curl,CURLOPT_HTTPHEADER,$ headers);
$ response = curl_exec($ curl);
error_log(curl_exec done。);
$ curl_errno = curl_errno($ curl);
$ curl_err = curl_error($ curl);
if($ curl_errno){
//打印错误
}
else {
error_log(Response:。
curl_close($ curl);
return json_decode($ response,true);
}
}
然后我在主页调用sendMail方法
$ send = OutlookService :: sendMail($ _ SESSION [access_token],$ _SESSION [user_email],testing < html>< body>测试电子邮件。< / body>< / html>,example@gmail.com);
echo var_dump($ send);
我可以知道我的代码有什么问题吗?
属性Content-Type在类型Microsoft.OutlookServices中不存在。 ItemBody',它应该是ContentType。有关详细信息,请参阅本文档:
https://msdn.microsoft.com/office/office365/api/complex-types-for-mail-contacts-calendar#ItemBody
如果要使用REST API发送邮件消息,还需要在Azure AD中为O365 Exchange Online以用户的方式发送邮件权限。您也可以参考以下文章了解更多详情:
https://dev.outlook。 com / restapi / tutorial / php
I try to send email from a signed Outlook Account with Outlook Rest Api and Curl then I get this error
Request returned status 400
This is my code for sending mail
private static $outlookApiUrl = "https://outlook.office.com/api/v2.0";
public static function sendMail ($access_token,$user_email,$subject,$Content,$email){
$arr= array(
"Message" =>array(
'Subject' => $subject,
"Body"=>array(
"Content-Type"=>"HTML",
"Content"=>$Content,
),
"ToRecipients"=>array(
array(
"EmailAddress"=>array(
"Address"=>$email,
)
),
),
));
$json=json_encode($arr, true);
$getMessagesUrl = self::$outlookApiUrl."/me/sendmail";
return self::makeApiCall($access_token, $user_email, "POST",$getMessageUrl,$json);
}
and this is the code for CURL
public static function makeApiCall($access_token, $user_email, $method, $url, $payload = NULL) {
// Generate the list of headers to always send.
$headers = array(
"User-Agent: php-tutorial/1.0",
"Authorization: Bearer ".$access_token,
"Accept: application/json",
"client-request-id: ".self::makeGuid(),
"return-client-request-id: true",
"X-AnchorMailbox: ".$user_email
);
$curl = curl_init($url);
switch(strtoupper($method)) {
case "POST":
error_log("Doing POST");
$headers[] = "Content-Type: application/json";
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
break;
default:
error_log("INVALID METHOD: ".$method);
exit;
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
error_log("curl_exec done.");
$curl_errno = curl_errno($curl);
$curl_err = curl_error($curl);
if ($curl_errno) {
//PRINT ERROR
}
else {
error_log("Response: ".$response);
curl_close($curl);
return json_decode($response, true);
}
}
THEN I call the sendMail method at Home page
$send=OutlookService::sendMail($_SESSION["access_token"], $_SESSION["user_email"],"testing","<html><body>testing email.</body></html>","example@gmail.com");
echo var_dump($send);
Can I know what's wrong with my code ? and why would I get this error?
The property 'Content-Type' does not exist on type 'Microsoft.OutlookServices.ItemBody' , It should be 'ContentType' . Please refer to this document for details : https://msdn.microsoft.com/office/office365/api/complex-types-for-mail-contacts-calendar#ItemBody
Also you need to 'send mail as a user' permission for O365 Exchange Online in Azure AD if you want to send a mail message with REST API . You could also refer to below article for more details: https://dev.outlook.com/restapi/tutorial/php
这篇关于Outlook Rest Api发送邮件请求返回状态400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!