Microsoft图表 - 无法在我的邮件

时间:2018-04-26 10:26:21

标签: php microsoft-graph outlook-restapi

所以,今天我又回到了你的另一个微软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"
    }
  }
}

我尝试了多个大写/小写名称,因为doc说它是这样写的,但人们使用不同的语法,任何建议吗?谢谢!

1 个答案:

答案 0 :(得分:0)

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

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