Thomas A
Thomas A

Reputation: 3

Microsoft graph - can't add attachments to my message

So, today I comeback to you with another microsoft API problem,

I'm able to send mails, without any troubles with microsoft graph api, but the problem comes when I want to add attachments, I followed the different doc, and even found people with same problems, but I keep getting the same error, here's the code:

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

and here's the error I'm getting:

{
  "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"
    }
  }
}

I tried multiple upper/lower case name since doc says it's written like this, but people made it work with different syntax, any suggestions? Thanks!

Upvotes: 0

Views: 2736

Answers (1)

Jason Johnston
Jason Johnston

Reputation: 17692

You need another set of [] around your attachment. The attachments property is an array of objects.

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

Upvotes: 2

Related Questions