How to download outlook attachment from Python Script?

前端 未结 3 1494
走了就别回头了
走了就别回头了 2021-01-05 07:48

I need to download incoming attachment without past attachment from mail using Python Script.

For example:If anyone send mail at this time(now) then just download th

3条回答
  •  感情败类
    2021-01-05 08:11

    The below code helps by downloading the attachments from outlook emails that are

    • 'Unread' (and changes the mail to Read.) or from 'Today's' date.
    • without altering the file name.

    Just pass the 'Subject' argument.

    import datetime
    import os
    import win32com.client
    
    
    path = os.path.expanduser("~/Desktop/Attachments")
    today = datetime.date.today()
    
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    inbox = outlook.GetDefaultFolder(6) 
    messages = inbox.Items
    
    
    def saveattachemnts(subject):
        for message in messages:
            if message.Subject == subject and message.Unread or message.Senton.date() == today:
                # body_content = message.body
                attachments = message.Attachments
                attachment = attachments.Item(1)
                for attachment in message.Attachments:
                    attachment.SaveAsFile(os.path.join(path, str(attachment)))
                    if message.Subject == subject and message.Unread:
                        message.Unread = False
                    break
    

提交回复
热议问题