msgraph-sdk-php
msgraph-sdk-php copied to clipboard
v2 plain text attachment arrives unreadable
Hello,
We're migrating code that was using v1 of the SDK to v2, however I'm running into an issue receiving attachments. The test code attaches a plain text file to the message but upon receiving the email, the file is unreadable.
The exact same file works fine with v1 of the SDK.
Sample code:
$attach = [];
foreach ($attachments as $path => $mimeType) {
if (!is_readable($path))
throw new \InvalidArgumentException("$path is not readable and cannot be attached.");
$attachment = new FileAttachment();
$attachment->setOdataType('#microsoft.graph.fileAttachment');
$attachment->setName(pathinfo($path, PATHINFO_BASENAME));
$attachment->setContentType($mimeType);
/* DEBUG */
print($attachment->getContentType() . "\n"); // "text/plain"
$resource = Utils::tryFopen($path, 'r');
$attachment->setContentBytes(Utils::streamFor($resource));
echo $attachment->getContentBytes()->getContents(); // "sample used for attachments" -- as expected
$attachment->getContentBytes()->rewind(); // for debugging
$attach[] = $attachment;
}
$message->setAttachments($attach);
File that gets attached: sample_attachment.txt
Received file: test_attachment_received.txt
Everything else in the received email is as expected.