Microsoft Graph - 无法向我的邮件添加附件

Microsoft graph - can't add attachments to my message

那么,今天我又给大家带来了另一个微软 API 问题,

我可以发送邮件,使用 microsoft graph 没有任何问题 api,但是当我想添加附件时,问题就来了,我按照不同的文档,甚至发现有人有同样的问题,但我不断收到同样的错误,这是代码:

$b64Doc = chunk_split(base64_encode(file_get_contents($path)));

//send mail
$payload = [
    'Message' => [
        'subject' => $subject,
        'body' => [
            'contentType' => 'Html',
            'content' => $body,
        ],
        'toRecipients' => [
            [
                'emailAddress' => [
                    'address' => $email,
                ]
            ]
        ],
        'attachments' => [
            '@odata.type' => '#microsoft.graph.fileAttachment',
            'Name' => 'file.pdf',
            'ContentBytes' => $b64Doc,
            'contentType' => 'application/pdf',
],
    ],
    'saveToSentItems' => "false",
];

$microsoft_message = $graph->createRequest("POST", "/me/sendmail")
    ->attachBody($payload)
    ->execute();

这是我遇到的错误:

{
  "error": {
    "code": "BadRequest",
    "message":
      "Property attachments in payload has a value that does not match schema.",
    "innerError": {
      "request-id": "c060b553-80f9-4f86-8784-291ff9be2082",
      "date": "2018-04-26T09:15:03"
    }
  }
}

我尝试了多个 upper/lower 案例名称,因为文档说它是这样写的,但是人们用不同的语法使它工作,有什么建议吗?谢谢!

您的附件还需要另一套 []attachments 属性 是一个对象数组。

'attachments' => [
    [
        '@odata.type' => '#microsoft.graph.fileAttachment',
        'Name' => 'file.pdf',
        'ContentBytes' => $b64Doc,
        'contentType' => 'application/pdf',
    ]
],