aboutsummaryrefslogtreecommitdiffstats
path: root/src/acit/smtpplugin.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/acit/smtpplugin.py')
-rw-r--r--src/acit/smtpplugin.py43
1 files changed, 29 insertions, 14 deletions
diff --git a/src/acit/smtpplugin.py b/src/acit/smtpplugin.py
index fafa892..0abb9eb 100644
--- a/src/acit/smtpplugin.py
+++ b/src/acit/smtpplugin.py
@@ -1,4 +1,6 @@
+from typing import Callable
+
import cherrypy
import threading
from os import getenv
@@ -42,7 +44,10 @@ class SmtpPlugin():
replytoall:bool=True,
tosend:IMAP_Message|Message=None,
save=False,
- extraheaders:dict={}
+ extraheaders:dict={},
+ overwriteheaders:dict={},
+ postprocessor:Callable=None,
+ smtp_to:list|str=None,
):
"""
Required arguments:
@@ -74,17 +79,12 @@ class SmtpPlugin():
tosend["Resent-From"]=from_
tosend["Resent-Date"]=utils.format_datetime(datetime.now())
tosend["Resent-To"]=", ".join(to)
+ tosend["Resent-Message-ID"]=self.gen_msg_id()
if bcc:
tosend["Resent-Bcc"]=", ".join(bcc)
if cc:
tosend["Resent-Cc"]=", ".join(bcc)
- if not from_ in tosend["to"]+tosend.get("cc","")+tosend.get("resent-to",""):
- if "cc" in tosend:
- tosend["cc"]+=", "+from_
- else:
- tosend["cc"]=from_
-
if not save:
tosend["X-Acit-Delete-When-Sender"]=from_
@@ -129,8 +129,8 @@ class SmtpPlugin():
tosend["Sender"]=from_
tosend["X-Acit-Is-Outgoing"]=from_
- if not "Message-ID" in tosend:
- tosend["Message-ID"]="<"+datetime.now().strftime("%d%m%Y%H%M%S%j")+"@"+self.domain+">"
+ if not "message-id" in tosend:
+ tosend["Message-ID"]=self.gen_msg_id()
if save and not from_ in tosend["Cc"]:
tosend["Cc"]+=" "+from_
@@ -138,11 +138,25 @@ class SmtpPlugin():
for key,value in extraheaders.items():
tosend[key]=value
- self.que.put(tosend)
+ for key,value in overwriteheaders.items():
+ del tosend[key]
+ tosend[key]=value
+
+ if callable(postprocessor):
+ postprocessor(tosend)
+
+ self.que.put({"msg":tosend,"real-to":smtp_to})
- return tosend["Message-ID"]
+ import traceback
+ function=traceback.extract_stack(limit=2)[0].name
+ self.mlog("sendmail called by",function,"for",tosend["message-id"])
+
+ return tosend["message-id"]
+ def gen_msg_id(self):
+ return "<"+datetime.now().strftime("%d%m%Y%H%M%S%j")+"@"+self.domain+">"
+
def format_reply_headers(self,replyto:IMAP_Message,tosend:Message|dict={}):
if "references" in replyto.headers:
tosend["References"]=replyto.obj["references"]
@@ -169,8 +183,8 @@ class SmtpPlugin():
smtp=None
while True:
try:
- item=self.que.get(timeout=1)
- if not item:
+ data=self.que.get(timeout=1)
+ if not data:
continue
except Empty:
if smtp:
@@ -181,6 +195,7 @@ class SmtpPlugin():
continue
try:
+ item=data["msg"]
if not smtp:
self.mlog("Connecting and logging in to smtp server")
smtp=SMTP(host=self.smtp_server,port=self.smtp_port)
@@ -189,7 +204,7 @@ class SmtpPlugin():
self.mlog("Sending message %s '%s'"%(item.get("message-id"),item.get("subject")))
- smtp.send_message(item)
+ smtp.send_message(item, to_addrs=data["real-to"])
except SMTPAuthenticationError:
self.mlog("!!! AUTH ERROR !!! please check your config. Discarding email.")