Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage Samples: Add Usage Documentation for Email Attachment Download #110

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DuncBegg
Copy link

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.

@github-actions
Copy link
Contributor

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

@Renji-FR
Copy link

Renji-FR commented May 2, 2023

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')

@CommunityHam
Copy link

CommunityHam commented May 25, 2023

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.

@Renji-FR
Copy link

@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)
    ...

@jussihi
Copy link

jussihi commented Sep 17, 2023

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

@baywet baywet requested a review from a team as a code owner June 19, 2024 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants