Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Outlook Emails identification and reading links using IMAP

I am trying to write a python3 code that logs in an Outlook Email account and then searches for Emails with some specific filters (FROM, SUBJECT, DATE) to fetch it then parse it and then take a specific link out of its body, using IMAP and BeautifulSoup.

I was able to do: 1- logging in my Email account and then my INBOX using IMAP. 2- fetch emails and then parsing them. 3- locating a link on a different .txt file

What I wasn't able to do is: searching for specific Emails using IMAP.

I was wondering if anyone can help me by showing the right syntaxes to search for certain Emails on Outlook.

Thank you in advance.

import imaplib
import email
from email.utils import parseaddr

username = '[email protected]'
password = 'Null'
Mymail = imaplib.IMAP4_SSL('outlook.office365.com')#imap- 
mail.outlook.com or outlook.office365.com
Mymail.login(username, password)
#Mymail.list() #OUT: list of "folders"
Mymail.select("INBOX") #connect to inbox

def get_body(msg):
    if msg.is_multipart():
        return get_body(msg.get_payload(0))
    else:
        return msg.get_payload(None,True)

def search(key, value, Mymail):
    result, data = Mymail.search(None, key, '"()"'.format(value))
    return data

result, data = Mymail.fetch(latest_email_id, '(RFC822)')
raw_email = email.message_from_bytes(data[0][1])

 print(search('FROM', '[email protected]'))
like image 448
Hadi K. Avatar asked Sep 06 '25 03:09

Hadi K.


1 Answers

I've been able to read emails from both Outlook and Gmail (and others) using IMAPClient and mailparser:

from imapclient import IMAPClient
import mailparser

with IMAPClient(self.host) as server:
    server.login(self.username, self.password)
    server.select_folder('INBOX')
    messages = server.search(['UNSEEN', ])  # in your case: ['FROM', '[email protected]']

    # for each unseen email in the inbox
    for uid, message_data in server.fetch(messages, 'RFC822').items():
        email_message = mailparser.parse_from_string(message_data[b'RFC822'])

You can then access the elements of the email as described in the mail-parser doc (link above). For example:

# parse html from email
soup = BeautifulSoup(email_message.body, "html.parser")
msg_body = soup.get_text()
like image 159
Simon TheChain Avatar answered Sep 07 '25 19:09

Simon TheChain