Kiota MS Graph SDK for PHP email attachments

33 Views Asked by At

we are using v2.3.0 and have successfully managed to authenticate and send an email. The issue we are having is with email attachments. The code for the attachments is as follows:

            $attachment = new FileAttachment();
            $attachment->setName($emailAttachment['fileName']);
            $attachment->setContentType(mime_content_type($emailAttachment['fileLocation']));
            $data = Utils::tryFopen($emailAttachment['fileLocation'], 'r');
            $resource = base64_decode($data);
            $attachment->setContentBytes(Utils::streamFor($resource));
            $this->attachments[] = $attachment;

The issue is the attachments are received as empty files. We have tested with JPG, PNG, PDF and Office files and all are the same result. The attachments look correct in Outlook when received:

enter image description here

However the files are zero bytes when saved to the desktop. We have one log stamp:

PHP Warning: base64_decode() expects parameter 1 to be string, resource given

Any ideas on how to get this working please?

1

There are 1 best solutions below

0
JamesB On

MS Graph needs the data encoded not decoded! Working code:

$attachment = new FileAttachment();
$attachment->setName($emailAttachment['fileName']);
$attachment->setContentType(mime_content_type($emailAttachment['fileLocation']));
$resource = base64_encode(file_get_contents($emailAttachment['fileLocation']));
$attachment->setContentBytes(Utils::streamFor($resource));
$this->attachments[] = $attachment;