Usage Samples: Add Usage Documentation for Email Attachment Download
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.
This pull request has conflicting changes, the author must resolve the conflicts before this pull request can be merged.
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')
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 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)
...
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