2016-05-31 115 views
1

我尝试从签订Outlook帐户发送电子邮件与Outlook REST API和卷曲,然后我得到这个错误展望REST API发送邮件请求返回的状态400

Request returned status 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"=>array(
       "Address"=>$email, 
      ) 
     ), 
     ), 
    )); 

    $json=json_encode($arr, true); 

    $getMessagesUrl = self::$outlookApiUrl."/me/sendmail"; 


    return self::makeApiCall($access_token, $user_email, "POST",$getMessageUrl,$json); 

} 

,这是卷曲

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); 
    } 
    } 

那么代码我称之为sendmail的方法在家PA ge

$send=OutlookService::sendMail($_SESSION["access_token"], $_SESSION["user_email"],"testing","<html><body>testing email.</body></html>","[email protected]"); 

    echo var_dump($send); 

我可以知道我的代码有什么问题吗?为什么我会得到这个错误?

回答

1

属性'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