expephalon-playsms/core/classes/mail.py
Klaus-Uwe Mitterer d549897186 Too many things to remember all of them
Finally got the mail queue working
Oh yeah, queueing
Added a TOTP provider which doesn't do anything much yet
Probably a hell of a lot of other things that I just can't remember
2020-05-13 12:38:37 +02:00

46 lines
1.7 KiB
Python

import smtplib
import email
import email.utils
from urllib.parse import urlparse
from dbsettings.functions import getValue
class BaseMailProvider:
@property
def get_name(self):
return "Base Mail Provider"
@property
def get_logo(self):
return ""
def send_message(self, message):
raise NotImplementedError(f"{type(self)} does not implement send_message()!")
def send_mail(self, subject, content, recipients, cc=[], bcc=[], headers={}, sender=getValue("core.mail.sender", "expephalon@localhost")):
message = email.message_from_string(content)
headers["From"] = sender
headers["To"] = recipients if type(recipients) == str else ",".join(recipients)
headers["Cc"] = cc if type(cc) == str else ",".join(cc)
headers["Bcc"] = bcc if type(bcc) == str else ",".join(bcc)
headers["Subject"] = subject
headers["Message-ID"] = email.utils.make_msgid("expephalon", urlparse(getValue("core.base_url", "http://localhost/").split(":")[1]).netloc)
headers["Date"] = email.utils.formatdate()
for header, value in headers.items():
if value:
message.add_header(header, value)
return self.send_message(message)
class SMTPMailProvider(BaseMailProvider):
def __init__(self, host=getValue("core.smtp.host", "localhost"), port=int(getValue("core.smtp.port", 0)), username=getValue("core.smtp.username", "") or None, password=getValue("core.smtp.password", "")):
self.smtp = smtplib.SMTP(host, port)
if username:
self.smtp.login(username, password)
@property
def get_name(self):
return "SMTP Mail"
def send_message(self, message):
return self.smtp.send_message(message)