I am working on a python program to permanently monitor and answer incoming emails that will periodically run in a container. The functionality initially was written for a much bigger program in C# but it seemed to me that rewriting it in python would be a suitable choice because it is comparibly small, would be more maintainable (read: not tied to me) and the process of making it reliably run in a container would be easier and faster than with a C# program.
The problem I've been rather confused about came about when I wanted to implement putting an email sent with SMTP into the INBOX.Sent folder with IMAP and I found no solution to use the already established IMAP4_SSL connection:
Module A (handling the IMAP connection):
...
mail = imaplib.IMAP4_SSL(imap_server, port=993)
mail.login(...)
...
def append_sent_email(email_raw_text: str):
mail.append('INBOX.Sent', '\\Seen', imaplib.Time2Internaldate(time.time()), email_raw_text.encode('utf8'))
Module B (handling the SMTP connection/sending the email):
import A
...
def create_and_send_email(to_email: str, subject: str, message: str):
...
smtp.sendmail(email_with_name, to_email, msg.as_string())
A.append_sent_email(msg.as_string())
There seems to be no way to make this work in python that is proportionate to the problem. Importing A in B just results in a second instance of mail
(which admittedly took far too long to debug). Not importing A in B means that append_sent_email
cannot be called. This is just one example of multiple similar issues I ran into. I don't use python in particular very often but I would like this to be maintainable by someone who doesn't code much. Is there any way to solve this other than redesigning everything?