Exemplo n.º 1
0
def main():

    with MailBox(IMAP_SERVER).login(EMAIL_ACCOUNT, EMAIL_PASSWORD, initial_folder='INBOX') as mailbox:
        for msg in mailbox.fetch(Q('UNSEEN')):
        #for msg in mailbox.fetch():

            mailBody = msg.text
            create_tmp_files(remove_umlaut(mailBody))
Exemplo n.º 2
0
 def _get_new_emails(self):
     new_emails = []
     for mailbox_name in self._config.sections():
         config = self._config[mailbox_name]
         with MailBox(config["url"]).login(config["address"],
                                           config["password"]) as mailbox:
             for msg in mailbox.fetch(Q(seen=False), mark_seen=False):
                 new_emails.append(msg)
     return new_emails
def search_email_inbox(search_string, username, password):
    search_from_date = dt.date.today() - dt.timedelta(days=90)

    # get list of email subjects from INBOX folder
    with MailBox('imap.gmail.com').login(username, password) as mailbox:
        for message in mailbox.fetch(Q(
                AND(subject=search_string, date_gte=search_from_date)),
                                     miss_defect=False,
                                     miss_no_uid=False):
            yield message.subject
Exemplo n.º 4
0
def get_email_from_last(num_days: 1, folders, email_info):
    Mail = namedtuple('Mail', ['from_', 'date', 'object', 'body'])
    mails_roundcube = []

    with MailBox(email_info.host).login(email_info.login, email_info.password) as mailbox:
        for folder in folders:
            mailbox.folder.set(folder)
            query_result = [Mail(msg.from_, msg.date,msg.subject, msg.text) 
                                for msg in mailbox.fetch(Q(date_gte=dt.date.today() - dt.timedelta(days=num_days)))]
            mails_roundcube = mails_roundcube + query_result
    
    return mails_roundcube  
Exemplo n.º 5
0
def find_attendance_messages(server, mail, password):
    mailbox = MailBox(server)
    mailbox.login(mail, password, initial_folder='inbox')
    f = open('attendance_messages.txt.', 'w')
    counter = 0
    for message in mailbox.fetch(Q(text='Attendance', date=date.today())):
        f.write(message.text)
        counter += 1
        f.write(
            '=====================================================================================\n'
        )
    f.write(f'\n{counter} URLS were generated.')
    f.close()
    mailbox.logout()
Exemplo n.º 6
0
def network_mailbox(action):
    """ Fetch mail box with stats and latest 10 mails dates/subjects"""
    config = Config("config.yaml")
    result = dict()
    if action in ("clear", "clearall"):
        with MailBox(config.mailbox_host).login(
                config.mailbox_username,
                config.mailbox_password,
                initial_folder=config.mailbox_folder,
        ) as mailbox:
            mailbox.delete([msg.uid for msg in mailbox.fetch()])
            return ("mailbox " + config.mailbox_name + " (" +
                    config.mailbox_folder + ")" + " has been cleared !")
    if action == "get":
        with MailBox(config.mailbox_host).login(
                config.mailbox_username,
                config.mailbox_password,
                initial_folder=config.mailbox_folder,
        ) as mailbox:
            msgcount = 0
            for msg in mailbox.fetch(Q(date=datetime.date.today()),
                                     reverse=True):
                print(msg)
                msgcount = msgcount + 1
            for msg in mailbox.fetch(Q(date=datetime.date.today()),
                                     limit=10,
                                     reverse=True):
                result[str(msg.date)] = msg.subject
                if msgcount > 0:
                    result["!!! "] = (
                        str(msgcount) +
                        " mail have been found in the mailbox !!!")
                return compute.compute_markdown_rendering(3, result)
            else:
                return (":):):) 0 mails in mailbox " + config.mailbox_name +
                        " (" + config.mailbox_folder + ")")
Exemplo n.º 7
0
def CheckMail():
  config = LoadConfig()
  ConfirmArchive(config)
  mailbox = MailBox(config['imap_ssl_host'], config['imap_ssl_port'])
  try:
    mailbox.login(config['email_address'], config['email_password'], initial_folder='Inbox')
  except:
    i=0
    while i < config['max_retries']:
      print("Failed to log in to mailbox to retrieve mail. Retrying...")
      print("Retry %d of max %d" % (i, config['max_retries']))
      sleep(1)
      try:
        mailbox.login(config['email_address'], config['email_password'], initial_folder='Inbox')
        break
      except:
        i=i+1
  
  for message in mailbox.fetch(Q(seen=False)):
    if config['allowed_senders']:
      sender = message.from_
      if sender not in config['allowed_senders']:
        continue
    subject = message.subject
    body = message.text
    video_url = body.strip()
    try:
      with youtube_dl.YoutubeDL(config['youtube-dl_options']) as ydl:
        ydl.download([video_url])
    except Exception as e:
      print("Reporting error to user via email.")
      ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
      error = ansi_escape.sub('', str(e))
      s= smtplib.SMTP(host=config['smtp_host'], port=config['smtp_port'])
      s.starttls()
      s.login(config['email_address'], config['email_password'])

      msg = MIMEMultipart()
      msg['From']=config['email_address']
      msg['To']=sender
      msg['Subject']="Re: %s" % subject
      msg.attach(MIMEText(error, 'plain'))
      s.send_message(msg)
      del msg
      s.quit()

  mailbox.logout()
Exemplo n.º 8
0
    def check_mail(self):
        link = ""
        mailbox = MailBox('imap.zoho.com')
        mailbox.login('*****@*****.**', 'memyself555', initial_folder='ZMNotification')
        mails = [msg for msg in mailbox.fetch(Q(text="Give your account the green light"))]

        for mail in mails:
            mail_to = mail.to[0]
            if mail_to == self.EMAIL:
                content = mail.text
                link = re.search('<(.*)>',content).group(1)                

        if link == "" :
            logger.info("didn't receive mail or mail problem")
            return self.check_mail()

        return link
Exemplo n.º 9
0
    def check_mail(self):
        logger.info("checking mail of " + self.EMAIL)
        link = ""
        mailbox = MailBox(self.IMAP_SERVER)
        mailbox.login(self.IMAP_USER, self.IMAP_PASS, initial_folder=self.IMAP_FOLDER)
        mails = [msg for msg in mailbox.fetch(Q(text=self.EMAIL))]

        for mail in mails:
            if "Activate" in mail.subject:
                content = mail.text
                link = re.search('<(.*)>',content).group(1)                

        if link == "" :
            logger.info("didn't receive mail or mail problem")
            return self.check_mail()

        return link
Exemplo n.º 10
0
    def fetchBox(self, folder=None, Query=None):
        '''Get list of emails from Mailbox server
        Folder-> Specific folder to extract emails
        Query-> Filter Emails from a Query
        :return List of mail objects'''
        if folder is None:
            folder = 'INBOX'
        if Query is None:
            Query = Q(all=True)
        mailbox = MailBox(self.imap_server, self.imap_port)
        mailbox.login(self.username, self.password, initial_folder=folder)

        message_list = []
        for mail in mailbox.fetch(Query):
            message_list.append(mail)
        mailbox.logout()
        return message_list
Exemplo n.º 11
0
    def check_mail(self):
        time.sleep(10)
        logger.info("checking mail of " + self.OTP_EMAIL)
        link = ""
        mailbox = MailBox(self.IMAP_SERVER)
        mailbox.login(self.IMAP_USER, self.IMAP_PASS, initial_folder=self.IMAP_FOLDER)
        try : 
            for mail in mailbox.fetch(Q(all=True)) :
                if mail.to[0] == self.OTP_EMAIL :
                    content = mail.text
                    for code in content.split() :
                        if code.isdigit():
                            link = code
        except:
            pass

        if link == "" :
            logger.info("didn't receive mail or mail problem")
            return self.check_mail()

        return link
Exemplo n.º 12
0
    def fetchBox(self, folder=None, Query=None):

        if folder is None:
            folder = 'INBOX'

        if Query is None:
            Query = Q(all=True)

        server = 'imap.' + self.username.split("@")[1]
        mailbox = MailBox(server)
        mailbox.login(
            self.username, self.password,
            initial_folder=folder)  # or mailbox.folder.set instead 3d arg

        message_list = []
        for mail in mailbox.fetch(Query):
            message_list.append(mail)

        return message_list

        mailbox.logout()
Exemplo n.º 13
0
def get_mail_from_last(num_days):
    folders = get_mailbox_folders()
    mails_roundcube = []
    with MailBox(LOP_HOST).login(LOP_LOGIN, LOP_PASS) as mailbox:
        for folder in folders:
            mailbox.folder.set(folder)
            mails = mailbox.fetch(
                Q(date_gte=dt.date.today() - dt.timedelta(days=num_days)))
            for mail in mails:
                try:
                    body = mail.html if (len(mail.html) > 0) else mail.text
                    query_result = Email_OP(from_=mail.from_,
                                            to_='; '.join(mail.to),
                                            date_=mail.date,
                                            subject_=mail.subject,
                                            body_=body)
                    mails_roundcube = mails_roundcube + [query_result]
                except Exception as e:
                    print('erreur :', e, 'from : ', mail.from_, " date : ",
                          mail.date)
    return mails_roundcube
Exemplo n.º 14
0
    def check_mail(self):
        time.sleep(10)
        logger.info("checking mail of " + self.OTP_EMAIL)
        link = ""
        mailbox = MailBox(self.IMAP_SERVER)
        mailbox.login(self.IMAP_USER,
                      self.IMAP_PASS,
                      initial_folder=self.IMAP_FOLDER)
        mails = [msg for msg in mailbox.fetch(Q(text=self.OTP_EMAIL))]

        for mail in mails:
            if "Proton Verification Code" in mail.subject:
                content = mail.text
                for code in content.split():
                    if code.isdigit():
                        link = code
                # link = re.search('<(.*)>',content).group(1)

        if link == "":
            logger.info("didn't receive mail or mail problem")
            return self.check_mail()

        return link
Exemplo n.º 15
0
def processMailbox():
    with MailBox(mailServer).login(login, password, initial_folder='INBOX') as mailbox:
        logging.debug(sg.subject for msg in mailbox.fetch())

        for msg in mailbox.fetch(Q(from_=senderAddr)):
            logging.debug(f'Betreff: {msg.subject} von {msg.date}')
            if len(msg.attachments) > 0:
                attachment = [i[0] for i in msg.attachments] + [i[1] for i in msg.attachments]

                if fileExt in attachment[0]:
                    logging.info(f'{attachment[0]} wird heruntergeladen...')
                    f = open(os.path.join(attPath, attachment[0]), "wb") #destFilename If the Filename will not be replaced
                    f.write(attachment[1])
                    f.close()
                    try:
                        mailbox.move(msg.uid, moveMail)
                        #mailbox.delete(msg.id)
                        logging.debug(f'Mail verschoben.')
                    except:
                        logging.debug(f'Mail mit dem Betreff {msg.subject} konnte nicht verschoben werden')
                        continue
                else:
                    logging.info(f'{attachment[0]} entspricht nicht dem Suchbegriff {fileExt}. Datei wurde nicht heruntergeladen')
                    try:
                        mailbox.move(msg.uid, 'INBOX/rejected')
                        logging.debug(f'Mail verschoben.')
                        continue
                    except:
                        logging.debug(f'Mail mit dem Betreff {msg.subject} konnte nicht verschoben werden')
                        continue
            else:
                logging.debug(f'Mail hat keinen Anhang')
                try:
                    mailbox.move(msg.uid, 'INBOX/rejected')
                except:
                    logging.debug(f'Mail mit dem Betreff {msg.subject} konnte nicht verschoben werden')
                    continue
 def get_new_emails(self):
     ''' Checks for new email. If there is new email, it will return the tuple (sender, subject)'''
     # I suppose one email will have been received, otherwise just pick the last email received
     try:
         mailbox = MailBox(self._email_server)
         mailbox.login(self._email_address,
                       self._email_password,
                       initial_folder='INBOX')
         received_emails = False
         subject = None
         sender = None
         for msg in mailbox.fetch(Q(seen=False)):
             received_emails = True
             subject = msg.subject.lower()
             sender = msg.from_
             #print(subject)
         mailbox.logout()
     except Exception:
         print("Some error occured while checking for new emails...")
         return (None, None)
     if received_emails:
         return (sender, subject)
     else:
         return (None, None)
Exemplo n.º 17
0
def processMails(imapserver: str,
                 username: str,
                 password: str,
                 imap_path: str,
                 directory_path: str,
                 numberOfMessages: int,
                 deleteAfterDownload: bool = False,
                 query: str = None) -> None:
    """
  imap_path should have a value like 'INBOX.2008', while
  directory_path is where the files (one for each message) get stored as *.eml file
  """
    # check if the output folder exists:
    # (right now, a terminal exception is being thrown if the folder doesn't exist)
    # numberOfMessages = 5000
    count = 1
    totalcount = 0
    # query = AND(date_gte=datetime.date(2020, 1, 1), date_lt=datetime.date(2020, 2, 4))
    # get list of email messages from the specified folder
    with MailBox(imapserver).login(username,
                                   password,
                                   initial_folder=imap_path) as mailbox:
        logger.info("Mailbox {}@{}/{} opened ... ".format(username,
                                                          imapserver,
                                                          imap_path,
                                                          sep=""))
        try:
            # Q(subject='Saludos'),
            # for msg in mailbox.fetch(query, limit=numberOfMessages, miss_no_uid=True, miss_defect=False):  # Q(all=True)
            if query == None:
                query = Q(all=True)
            for msg in mailbox.fetch(query,
                                     limit=numberOfMessages,
                                     miss_no_uid=False,
                                     miss_defect=False,
                                     mark_seen=False):  # Q(all=True)
                totalcount += 1
                # sometimes there's an attribute error in the following line because the mail address cannot be parsed:
                name = msg.from_
                name = name.replace(
                    '/', '-'
                )  # Deutsche Bahn puts LDAP info in their mail addresses ....
                # remove special characters from the subject line:
                subject = msg.subject.replace('/', '-').replace(
                    ' ', '_').replace('?', '_').replace('\x00', '').replace(
                        '\x09',
                        '').replace('\x08',
                                    '').replace('\x0A',
                                                '').replace('\x0D', '')
                filename = "{}/{}_{}_({})_{}.eml".format(
                    directory_path,
                    datetime.datetime.strftime(msg.date, '%Y-%m-%d_%H-%M-%S'),
                    name, msg.uid, subject[0:100]).replace('\x00', '').replace(
                        '\x09',
                        '').replace('\x08',
                                    '').replace('\x0A',
                                                '').replace('\x0D', '')
                # logger.debug("{}/{}: Processing {} -> {}, {} ...".format(totalcount, numberOfMessages, name, msg.to, subject))
                if not os.path.isfile(filename):
                    with open(filename, 'x', encoding='utf-8') as f:
                        logger.info("{}/{}: Writing {} ...".format(
                            count, numberOfMessages, filename))
                        f.write(
                            msg.obj.as_bytes().decode(encoding='ISO-8859-1'))
                        if deleteAfterDownload:
                            mailbox.delete(msg.uid)
                            logger.debug("Deleted message uid {} ...".format(
                                msg.uid))
                        count += 1
                    # set the time of the created file to the time of the e-mail:
                    ts = msg.date.timestamp()
                    os.utime(filename, (ts, ts))
                else:
                    logger.warn("File {} already exists!".format(filename))
        except (RuntimeError, AttributeError) as error:
            logger.error("Error while processing message uid {}: {}".format(
                msg.uid, error))
            traceback.print_last()
Exemplo n.º 18
0
    "NOT (((OR OR FROM "1" TO "22" TEXT "33") CC "44" BCC "55"))"
"""

import datetime as dt
from imap_tools import AND, OR, NOT, Q, H

# date in the date list (date=date1 OR date=date3 OR date=date2)
q1 = OR(date=[dt.date(2019, 10, 1), dt.date(2019, 10, 10), dt.date(2019, 10, 15)])
# "(OR OR ON 1-Oct-2019 ON 10-Oct-2019 ON 15-Oct-2019)"

# date not in the date list (NOT(date=date1 OR date=date3 OR date=date2))
q2 = NOT(OR(date=[dt.date(2019, 10, 1), dt.date(2019, 10, 10), dt.date(2019, 10, 15)]))
# "NOT ((OR OR ON 1-Oct-2019 ON 10-Oct-2019 ON 15-Oct-2019))"

# subject contains "hello" AND date greater than or equal dt.date(2019, 10, 10)
q3 = Q(subject='hello', date_gte=dt.date(2019, 10, 10))
# "(SUBJECT "hello" SINCE 10-Oct-2019)"

# from contains one of the address parts
q4 = OR(from_=["@spam.ru", "@tricky-spam.ru"])
# "(OR FROM "@spam.ru" FROM "@tricky-spam.ru")"

# marked as seen and not flagged
q5 = AND(seen=True, flagged=False)
# "(SEEN UNFLAGGED)"

# (text contains tag15 AND subject contains tag15) OR (text contains tag10 AND subject contains tag10)
q6 = OR(AND(text='tag15', subject='tag15'), AND(text='tag10', subject='tag10'))
# "(OR (TEXT "tag15" SUBJECT "tag15") (TEXT "tag10" SUBJECT "tag10"))"

# (text contains tag15 OR subject contains tag15) OR (text contains tag10 OR subject contains tag10)
Exemplo n.º 19
0
username = "******"
password = "******"
imap = imaplib.IMAP4_SSL("imap.gmail.com")
mailbox = MailBox("imap.gmail.com")
imap.login(username, password)
mailbox.login(username, password, initial_folder="INBOX")
status, numberMessages = imap.select()

#date from which we want the emails
year = 2020
day = 8
month = 5

#fetches Best Buy emails
bestBuyMessages = mailbox.fetch(
    Q(from_="*****@*****.**",
      date_gte=datetime.date(year, month, day)))

#creates output dataframe that will be added to
orderConfirmations = pd.DataFrame(index=['BBY01'],
                                  columns=[
                                      'Shipping #', 'Date',
                                      'Product # Quantity', 'Destination',
                                      'Cost'
                                  ])
shippedOrders = pd.DataFrame(index=['BBY01'],
                             columns=[
                                 'Shipping #', 'Date', 'Product # Quantity',
                                 'Destination', 'Cost'
                             ])
#orderConfirmations = pd.read_csv(r'orderConfirmations.csv', index_col = 0)
#shippedOrders = pd.read_csv(r'shippedOrders.csv')
Exemplo n.º 20
0
def mail_get_newswletter_text(mailbox):
    for msg in mailbox.fetch(Q(seen=False)):
        if "[\U0001f40dPyTricks]" in msg.subject:
            return mail_parse_newswletter_text(msg)
    return ""
Exemplo n.º 21
0
def mail_get_newswletter_text(mailbox):
    for msg in mailbox.fetch(Q(seen=False)):
        if "[\U0001f40dPyTricks]" in msg.subject and msg.from_ == "*****@*****.**":
            return mail_parse_newswletter_text(msg)
    return ""