|
@@ -1,4 +1,4 @@
|
|
|
-from imap_tools import MailBox, AND
|
|
|
+from imap_tools import MailBox, AND, MailMessage
|
|
|
import re
|
|
|
import json
|
|
|
import os
|
|
@@ -7,7 +7,7 @@ from datetime import datetime, date
|
|
|
|
|
|
|
|
|
class Imap:
|
|
|
- commands = ["cleanup", "move", "remove", "add_to_whitelist"]
|
|
|
+ commands = ["cleanup", "move", "remove", "add_to_whitelist", "undelivered"]
|
|
|
whitelist = {}
|
|
|
blacklist = []
|
|
|
|
|
@@ -114,7 +114,6 @@ class Imap:
|
|
|
mark_seen=False,
|
|
|
bulk=True,
|
|
|
limit=1000,
|
|
|
- headers_only=True,
|
|
|
)
|
|
|
selected = []
|
|
|
for msg in messages:
|
|
@@ -126,7 +125,56 @@ class Imap:
|
|
|
def remove(self):
|
|
|
self.remove_absence_messages()
|
|
|
|
|
|
+ def undelivered(self):
|
|
|
+ with MailBox("mail.global-cube.com").login("versand", "y6!avXX3tQvr") as mb:
|
|
|
+ selected = []
|
|
|
+ for msg in mb.fetch(
|
|
|
+ criteria=AND(to="@global-cube.com"),
|
|
|
+ mark_seen=False,
|
|
|
+ bulk=True,
|
|
|
+ limit=1000,
|
|
|
+ ):
|
|
|
+ for k in ["undeliverable", "returned", "zurückgeschickt"]:
|
|
|
+ if k in msg.subject.lower():
|
|
|
+ match = re.search(
|
|
|
+ r"<([\w\-\+\.]+@[\w\-\.]+\.[\w\-\.]+)>(.*)",
|
|
|
+ msg.text,
|
|
|
+ re.DOTALL,
|
|
|
+ )
|
|
|
+ if match:
|
|
|
+ mail_to = match[1]
|
|
|
+ text = self.get_error_message(match[2])
|
|
|
+
|
|
|
+ selected.append(";".join([mail_to, text]))
|
|
|
+ if len(msg.attachments) > 0:
|
|
|
+ msg2 = MailMessage.from_bytes(
|
|
|
+ msg.attachments[0].payload
|
|
|
+ )
|
|
|
+ selected[-1] += ";" + ";".join(
|
|
|
+ [msg2.from_, msg2.subject, msg2.date_str]
|
|
|
+ )
|
|
|
+ with open(os.path.dirname(__file__) + "/undelivered.csv", "w") as fwh:
|
|
|
+ fwh.write("\n".join(selected))
|
|
|
+
|
|
|
+ def get_error_message(self, text):
|
|
|
+ m1 = re.search(r"550 5.1.1 (Mailbox <.*> does not exist)", text)
|
|
|
+ if m1:
|
|
|
+ return m1[1]
|
|
|
+ m2 = re.search(r"(Host or domain name not found)", text)
|
|
|
+ if m2:
|
|
|
+ return m2[1]
|
|
|
+ m3 = re.search(r"550 5.1.1 (User unknown: .*)\(", text)
|
|
|
+ if m3:
|
|
|
+ return m3[1]
|
|
|
+ m4 = re.search(r"550 5.1.1 (.*)", text)
|
|
|
+ if m4:
|
|
|
+ return m4[1].replace("\n", "").replace("\r", "")
|
|
|
+ m5 = re.search(r"(X-Microsoft-Antispam)", text)
|
|
|
+ if m5:
|
|
|
+ return m5[1]
|
|
|
+ return text[:100]
|
|
|
+
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- plac.Interpreter.call(Imap)
|
|
|
- # Imap().remove_absence_messages()
|
|
|
+ # plac.Interpreter.call(Imap)
|
|
|
+ Imap().undelivered()
|