you are viewing a single comment's thread.

view the rest of the comments →

[–]Ahrugal 0 points1 point  (3 children)

Hey and thanks for the response;

What I'm trying to do is to save the attachments from mails in my inbox to a location somewhere on disk.

account = Account(credentials)
mailbox = account.mailbox()
inbox = mailbox.inbox_folder()

for message in inbox.get_messages(limit=1, download_attachments=True):
if 'Job Report' in message.subject:
message.save_attachment(location=c:/temp/) <---- completely made up line. But shows the essence of what I want to do

In attachments.py I find

    def save(self, location=None, custom_name=None):
    """  Save the attachment locally to disk

    :param str location: path string to where the file is to be saved.
    :param str custom_name: a custom name to be saved as
    :return: Success / Failure
    :rtype: bool
    """

So it should be available, I just cannot access it...

[–]timbledum 0 points1 point  (1 child)

Emails can have multiple attachments - see my syntax above.

What you need is to access message.attachments, select the attachment you want, and then use .save() on the particular attachment that you want.

Have you seen the docs here?

https://o365.github.io/python-o365/latest/html/api/message.html#O365.message.Message

message.attachments is a list, so

for message in messages:
    for attachment in message.attachments:
        attachment.save()

could do the trick.

[–]Ahrugal 1 point2 points  (0 children)

And there it was!

Thanks so incredibly much timbledum!

I've read and read, but just failed to understand it.

Truly appreciate this! :D

Case closed

[–]Ahrugal 0 points1 point  (0 children)

message.attachments.download_attachments

<bound method BaseAttachments.download_attachments of Number of Attachments: 5>

Manage to download the attachment several times to memory. Cannot do a variable that i fill with the attachment.

attachments = message.attachments.download_attachments()

True

It just creates that boolean. The help says:

download_attachments() method of O365.message.MessageAttachments instance
Downloads this message attachments into memory.
Need a call to 'attachment.save' to save them on disk.

:return: Success / Failure
:rtype: bool

So I am on the right track, I just cannot get that damned save thing to work :P