2016-05-31 112 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