code: def run(self, **kwargs):
          
          try:
              # connect to server and login
              box = imaplib.IMAP4_SSL(settings.IMAP_SERVER)
              box.login(settings.IMAP_USERNAME, settings.IMAP_PASSWORD)
              box.select()
  
              # search for all mails in the mailbox
              result, mail_indices = box.search(None, 'ALL')
  
              # if everything was ok...
              if result == 'OK':
  
                  # check number of mails
                  mail_count = len(mail_indices[0].split())
                  logger.info('found %(mail_count)d mails...' % {'mail_count': mail_count})
  
                  # iterate the mail indices and fetch the mails
                  ips_created = 0
                  for mail_index in mail_indices[0].split():
                      logger.info('fetching mail %(mail_index)s...' % {'mail_index': int(mail_index)})
                      # mail data is a list with a tuple
                      sub_result, mail_data = box.fetch(mail_index, '(BODY[TEXT])')
                      if sub_result == 'OK':
  
                          # fetch the ips
                          ips = list_remove_duplicates(
                              self.find_ips(''.join([str(data) for data in mail_data[0]]))
                          )
  
                          # if ips found, add them and delete the mail
                          if len(ips) > 0:
                              logger.info('found %(count)d IPs' % {'count': len(ips)})
                              ips_created += IP.batch_add_ips(ips)
                              box.store(mail_index, '+FLAGS', '\\Deleted')
  
                      else:
                          logger.error('fetching mail with index %(index)d failed' % {'index': mail_index})
  
                  # finally, if ips were added, unify the IPs
                  if ips_created > 0:
                      logger.info('created %(count)d IPs' % {'count': ips_created})
                      IP.unify_ips()
  
              else:
                  logger.error('search returned not OK')
  
              box.close()
              box.logout()
          except:
              logger.exception('retrieving mail failed')
docs: Checks the IMAP mailbox for new mails and tries to handle them.