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]'))
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With