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

Usage Samples: Add Usage Documentation for Email Attachment Download

Open DuncBegg opened this issue 2 years ago • 5 comments

Pls provide a sample showing how to download a file attachment in an email.

eg Say i wanted to download an excel file from my mailbox.

DuncBegg avatar Feb 21 '23 06:02 DuncBegg

This pull request has conflicting changes, the author must resolve the conflicts before this pull request can be merged.

github-actions[bot] avatar Mar 22 '23 10:03 github-actions[bot]

See https://github.com/microsoftgraph/msgraph-sdk-python/issues/92#issuecomment-1531213910

Example:

request_message = self._msgraph_request.messages.by_message_id(message_id)
request_attachment = request_message .attachments.by_attachment_id(attachment_id)

query_params = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetQueryParameters()

request_config = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetRequestConfiguration(
    query_parameters=query_params
)

attachment = await request_attachment .get(
    request_configuration=request_config
)

content = attachment.content_bytes
content = base64.b64decode(content)
content = content.decode('ascii')

Renji-FR avatar May 02 '23 10:05 Renji-FR

See #92 (comment)

Example:

request_message = self._msgraph_request.messages.by_message_id(message_id)
request_attachment = request_message .attachments.by_attachment_id(attachment_id)

query_params = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetQueryParameters()

request_config = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetRequestConfiguration(
    query_parameters=query_params
)

attachment = await request_attachment .get(
    request_configuration=request_config
)

content = attachment.content_bytes
content = base64.b64decode(content)
content = content.decode('ascii')

How are you getting the value for "attachment_ID" in this scenario? I cannot find any way to pull any information about the attachment other than "hasattachment", which is more a property of a message than an attachment. When I use the ".attachments" property on a mailitem, say, like, "message.attachments.ID", for example, it comes up Nonetype. I know I've authenticated properly, because I am still able to pull data from the body of the email, but cannot pull the attachment.

CommunityHam avatar May 25 '23 20:05 CommunityHam

@CommunityHam Hi, I use that:

async def _list_attachments(self, message:Message) -> None|list[Attachment]:
        query_params = AttachmentsRequestBuilder.AttachmentsRequestBuilderGetQueryParameters(
        )

        request_config = AttachmentsRequestBuilder.AttachmentsRequestBuilderGetRequestConfiguration(
            query_parameters=query_params
        )

        request_message = self._msgraph_request.messages.by_message_id(message.id)

        attachments:AttachmentCollectionResponse = await request_message.attachments.get(
            request_configuration=request_config
        )

        if attachments is not None and attachments.value is not None:
            if attachments.odata_next_link is not None:
                pass
            return attachments.value
        else:
            return None

Then use the return value like this:

attachments = await self._list_attachments(message)

if attachments is None:
    return None

query_params = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetQueryParameters(
)

request_config = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetRequestConfiguration(
        query_parameters=query_params
)

request_message = self._msgraph_request.messages.by_message_id(message.id)

for attachment in attachments:
    request_attachment = request_message.attachments.by_attachment_id(attachment.id)
    ...

Renji-FR avatar May 26 '23 00:05 Renji-FR

Can't you get all the attachments to a single list with single call:

attachments = (await (graph_client.users.by_user_id(user_id).messages.by_message_id(message.id).attachments.get())).value

then you can check if entry is an ItemAttachment and get its content_bytes

jussihi avatar Sep 17 '23 17:09 jussihi