Continuously run a python imaplib program alongside flask application

I am looking to run this python code that searches through an email account for messages once every 10 seconds alongside a flask application. I want to expand this code to access the same database as a flask application with live updating. Is this possible? Thanks in advance for any answers!

import email
import imaplib
EMAIL = ''
PASSWORD = 'password'
mail = imaplib.IMAP4_SSL(SERVER)
mail.login(EMAIL, PASSWORD)'inbox')
status, data =, 'ALL')
mail_ids = []
for block in data:
    mail_ids += block.split()

for i in mail_ids:
    status, data = mail.fetch(i, '(RFC822)')
    for response_part in data:
        if isinstance(response_part, tuple):
            message = email.message_from_bytes(response_part[1])
            mail_from = message['from']
            mail_subject = message['subject']
            if message.is_multipart():
                mail_content = ''
                for part in message.get_payload():
                    if part.get_content_type() == 'text/plain':
                        mail_content += part.get_payload()
                mail_content = message.get_payload()

            print(f'From: {mail_from}')
            print(f'Subject: {mail_subject}')
            print(f'Content: {mail_content}')

Read more here:

Content Attribution

This content was originally published by Python 123 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: