msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

Get email attachment that is itself an email

Open JacoSJKruger opened this issue 2 years ago • 2 comments

In the new version when an email has an attachment, but that attachment is itself an email, then this is handled as an 'Item'.

It does not have the standard properties like ContentBytes and ContentId. I would however like to download this file/item programatically but things like $emailAttachment->getContentBytes(); do not work.

I've also tried getting creative like:

$fileContents = $graphServiceClient->users()->byUserId($user_id)->messages()->byMessageId($email->getId())->attachments()->byAttachmentId($emailAttachment->getId())->content()->get()->wait();

But alas, still not working. Any ideas?

JacoSJKruger avatar Dec 11 '23 17:12 JacoSJKruger

Hi @JacoSJKruger,

I've also encountered this issue, and found a workaround for this. First of all I build the request as usual. In stead of calling the ->get() parameter, I obtain the requestInformation. After that I change the urlTemplate by adding the /$value parameter, and use that object in the sendPrimitiveAsync() method and let it return a string, to get the actual content of the attachment.

Not ideal, but I didn't find any other way in the current SDK to get the actual content.

$getRequest = $graphServiceClient
    ->users()
    ->byUserId($userId)
    ->messages()
    ->byMessageId($messageId)
    ->attachments()
    ->byAttachmentId($attachmentId)
    ->toGetRequestInformation();

// Change the urlTemplate to add the $value parameter
$getRequest->urlTemplate .= '/%24value';

$attachmentContent = $graphServiceClient
    ->getRequestAdapter()
    ->sendPrimitiveAsync($getRequest, 'string')
    ->wait();

inserve-paul avatar Feb 19 '24 13:02 inserve-paul