php - Outlook Rest Api 发送邮件请求返回状态 400

标签 php json email curl outlook-restapi

我尝试使用 Outlook Rest Api 和 Curl 从已签名的 Outlook 帐户发送电子邮件,然后收到此错误

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

 } 

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

然后我在主页调用 sendMail 方法

  $send=OutlookService::sendMail($_SESSION["access_token"], $_SESSION["user_email"],"testing","<html><body>testing email.</body></html>","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbaeb3aaa6bba7ae8baca6aaa2a7e5a8a4a6" rel="noreferrer noopener nofollow">[email protected]</a>");

    echo var_dump($send);

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

最佳答案

类型“Microsoft.OutlookServices.ItemBody”上不存在属性“Content-Type”,它应该是“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

关于php - Outlook Rest Api 发送邮件请求返回状态 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37546885/

相关文章:

iphone - 如何使用 json 解析将选定的图像发布到 Web 链接

php - javascript 对象到 json 字符串到 php 数组 -> POST

java - 如何检测gmail中主题行更改的电子邮件作为回复?

python - 为什么使用 Flask Mail 发送的文本附件是空的?

php - 将值从数组插入数据库

php - LEFT JOIN 不显示 NULL 结果

javascript - 使用 PHP 包装器从 wkhtml 生成 PDF,发送回 Angular 4 时,仅在 Chrome 中打开

json - 如何使用逗号格式化长值

python - 使用 Scrapy 通过电子邮件发送项目和日志

php - Wordpress CORS - 共享字体