imap.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from imap_tools import MailBox, AND
  2. import re
  3. whitelist = {
  4. '@auto-greiner.de': [
  5. '@opel-greiner.de'
  6. ],
  7. '@autohaus-berkemeier.de': [
  8. '@auto-berkemeier.de'
  9. ],
  10. '@mvcmotors.at': [
  11. '@mvcautomotive.com'
  12. ]
  13. }
  14. def main():
  15. mb = MailBox('mail.global-cube.com').login('archiv', 'Gc01am64!')
  16. messages = mb.fetch(criteria=AND(from_="@global-cube.com"), mark_seen=True, bulk=True)
  17. # print(mb.folder.list())
  18. # for folder in mb.folder.list():
  19. # if '|' in folder.name:
  20. # print(folder)
  21. # new_name = folder.name.replace('|', '.') # .replace('@', '').replace('.', '_')
  22. # mb.folder.rename(folder.name, new_name)
  23. for msg in messages:
  24. match = re.findall(r'\+(.*)@', msg.from_)
  25. if not match:
  26. print(msg.from_, msg.to, msg.subject)
  27. else:
  28. domain = '@' + match[0]
  29. subfolder = 'Archive.' + domain.replace('@', '').replace('.', '_')
  30. if not mb.folder.exists(subfolder):
  31. mb.folder.create(subfolder)
  32. mb.move(msg.uid, subfolder)
  33. for to in msg.to:
  34. if domain not in to and '@global-cube.de' not in to and msg.from_ != to:
  35. if domain in whitelist:
  36. valid_domain = [entry in to for entry in whitelist[domain]]
  37. if any(valid_domain):
  38. break
  39. print(domain, to, msg.subject)
  40. # print(msg.text)
  41. # print([att.filename for att in msg.attachments if att.filename.endswith('.pdf')])
  42. if __name__ == '__main__':
  43. main()