2020-04-18 13:02:41 +00:00
|
|
|
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
|
2020-05-13 10:38:37 +00:00
|
|
|
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)
|
2020-04-18 13:02:41 +00:00
|
|
|
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():
|
2020-05-13 10:38:37 +00:00
|
|
|
if value:
|
|
|
|
message.add_header(header, value)
|
|
|
|
return self.send_message(message)
|
2020-04-18 13:02:41 +00:00
|
|
|
|
|
|
|
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):
|
2020-05-13 10:38:37 +00:00
|
|
|
return self.smtp.send_message(message)
|