samedi 2 juillet 2016

Replying To email via Outlook in Python


I am trying to reply to emails with a keyword in the subject but I need to do it all through outlook. My current code works decently but it would be better if it could reply directly instead of creating a new message.

Hopefully this is the right place to ask this :)

    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    o = win32com.client.Dispatch("Outlook.Application")

    inbox = outlook.GetDefaultFolder(6)    
    def check_mail():
        global message
        messages = inbox.Items
        message = messages.GetLast()
        if (message.subject.find('@Bot') != -1 and message.unread and whtlist.find(message.SenderName)!= -1 ):
            return 1
        else:
            return 0

    def Read_mail():
        global message
        global ACTIVE
        body_content = message.body
        print(bcolors.WARNING+'n______________________________________________________________________n'+bcolors.OKGREEN)
        print (body_content)
        print(bcolors.WARNING+'n______________________________________________________________________n'+bcolors.OKGREEN)
        for att in message.Attachments:
            break
        try:
            att.SaveAsFile(os.getcwd() + '\new.xlsx')
            print(os.getcwd())
        except :
            print(bcolors.WARNING+'No Attachment Found'+bcolors.OKGREEN)
        message.unread=False
        Msg = o.CreateItem(0)
        Msg.To = message.SenderEmailAddress
        print(bcolors.FAIL+'Reply sent to: {}'.format(message.SenderEmailAddress+bcolors.OKGREEN))
        Msg.Subject = 'Autoreply'
        Msg.Body = 'I see you {}.nnTestingn-Bot'.format(message.SenderName)
        Msg.Send()

Aucun commentaire:

Enregistrer un commentaire