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.py62
1 files changed, 39 insertions, 23 deletions
diff --git a/src/acit/smtpplugin.py b/src/acit/smtpplugin.py
index 87285b2..8c47f75 100644
--- a/src/acit/smtpplugin.py
+++ b/src/acit/smtpplugin.py
@@ -39,8 +39,10 @@ class SmtpPlugin():
cc:list[str]=[],
bcc:list[str]=[],
replyto:IMAP_Message=None,
+ replytoall:bool=True,
tosend:IMAP_Message|Message=None,
save=False,
+ extraheaders:dict={}
):
"""
Required arguments:
@@ -59,7 +61,7 @@ class SmtpPlugin():
body (str): the email body
cc (list): email addresses to put in the cc
- save (bool): force adding from_ to the cclist, making ImapPlugin store the email in ``Sent`` on arrival
+ save (bool): add from_ to the cclist, making ImapPlugin store the email in ``Sent`` on arrival
"""
if type(to)==str:
@@ -83,27 +85,31 @@ class SmtpPlugin():
else:
tosend["cc"]=from_
- tosend["X-Acit-Delete-When-Sender"]=from_
+ if not save:
+ tosend["X-Acit-Delete-When-Sender"]=from_
else:
if replyto:
- cc.extend(replyto.cc)
- cc.extend(replyto.to)
- if not save:
- cc.remove(from_)
+ if replytoall:
+ cc.extend(replyto.cc)
+ cc.extend(replyto.to)
+
+ if from_ in cc: cc.remove(from_)
+ if from_ in bcc: bcc.remove(from_)
+
to.append(replyto.headers["reply-to"] if "reply-to" in replyto.headers else replyto.from_)
if not subject:
subject="Re: "+replyto.subject
- for addr in to:
- if addr in cc:
- cc.remove(addr)
for addr in cc:
if addr in bcc:
bcc.remove(addr)
+ for addr in to:
+ if addr in cc:
+ cc.remove(addr)
if not (to and body and from_ and subject):
raise ValueError("Missing to, from_, body, or subject")
@@ -112,22 +118,11 @@ class SmtpPlugin():
tosend["From"]=from_
tosend["To"]=", ".join(set(to)) # set()s can't have duplicates
tosend["Subject"]=subject
- tosend["Cc"]=", ".join(set(cc))
- tosend["Bcc"]=", ".join(set(bcc))
+ if cc: tosend["Cc"]=", ".join(set(cc))
+ if bcc: tosend["Bcc"]=", ".join(set(bcc))
if replyto:
- if "references" in replyto.headers:
- tosend["References"]=replyto.obj["references"]
- elif "in-reply-to" in replyto.headers:
- tosend["References"]=replyto.obj["in-reply-to"]
-
- if "message-id" in replyto.headers:
- tosend["In-Reply-To"]=replyto.obj["message-id"]
-
- if "References" in tosend:
- tosend["References"]+=" "+replyto.obj["message-id"]
- else:
- tosend["References"]=replyto.obj["message-id"]
+ self.format_reply_headers(replyto=replyto,tosend=tosend) # tosend is mutable so we can ignore the return value
tosend.set_payload(body)
@@ -140,9 +135,29 @@ class SmtpPlugin():
if save and not from_ in tosend["Cc"]:
tosend["Cc"]+=" "+from_
+ for key,value in extraheaders.items():
+ tosend[key]=value
+
self.que.put(tosend)
return tosend["Message-ID"]
+
+
+ def format_reply_headers(self,replyto:IMAP_Message,tosend:Message|dict={}):
+ if "references" in replyto.headers:
+ tosend["References"]=replyto.obj["references"]
+ elif "in-reply-to" in replyto.headers:
+ tosend["References"]=replyto.obj["in-reply-to"]
+
+ if "message-id" in replyto.headers:
+ tosend["In-Reply-To"]=replyto.obj["message-id"]
+
+ if "References" in tosend:
+ tosend["References"]+=" "+replyto.obj["message-id"]
+ else:
+ tosend["References"]=replyto.obj["message-id"]
+
+ return tosend
def start(self):
@@ -191,6 +206,7 @@ class SmtpPlugin():
import traceback
self.mlog("An error occured in the SMTP runner:\n",traceback.format_exc())
self.mlog("As a result, the following email was discarded: %s '%s'"%(item.get("message-id"),item.get("subject")))
+ self.mlog("Raw:\n"+item.as_string())
finally:
self.que.task_done()