2020-05-21 12:54:59 +00:00
|
|
|
from core.exceptions.mail import NoSuchTemplate
|
2020-04-18 13:02:41 +00:00
|
|
|
|
|
|
|
from dbsettings.functions import getValue
|
|
|
|
|
2020-05-21 12:54:59 +00:00
|
|
|
import os.path
|
|
|
|
|
2020-04-18 13:02:41 +00:00
|
|
|
def get_provider_by_name(name, fallback=True):
|
2020-05-24 15:44:27 +00:00
|
|
|
from core.modules.mail import providers
|
2020-04-18 13:02:41 +00:00
|
|
|
return providers.get(name, None) or providers["smtp"]
|
|
|
|
|
|
|
|
def get_default_provider(fallback=True):
|
|
|
|
return get_provider_by_name(getValue("core.email.provider", "smtp"), fallback)
|
|
|
|
|
2020-05-13 10:38:37 +00:00
|
|
|
def send_mail(provider=get_default_provider(), **kwargs):
|
2020-05-24 15:44:27 +00:00
|
|
|
from core.tasks.mail import send_mail as send_mail_task
|
2020-05-13 10:38:37 +00:00
|
|
|
provider = get_provider_by_name(provider) if type(provider) == str else provider
|
|
|
|
return send_mail_task.delay(provider, **kwargs)
|
|
|
|
|
|
|
|
def simple_send_mail(subject, content, recipients, cc=[], bcc=[], headers={}):
|
|
|
|
return send_mail(subject=subject, content=content, recipients=recipients, cc=cc, bcc=bcc, headers=headers)
|
|
|
|
|
2020-05-21 12:54:59 +00:00
|
|
|
def get_template(template_name, format="txt", **kwargs):
|
2020-05-24 15:44:27 +00:00
|
|
|
from core.modules.mail import templates
|
2020-05-21 12:54:59 +00:00
|
|
|
try:
|
|
|
|
template = templates[template_name][format]
|
|
|
|
except KeyError:
|
|
|
|
raise NoSuchTemplate(f"No email template called {template_name} of format {format} loaded")
|
|
|
|
|
|
|
|
with open(template, "r") as templatefile:
|
|
|
|
templatetext = templatefile.read()
|
|
|
|
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
templatetext = templatetext.replace('{§%s§}' % key, value)
|
|
|
|
|
|
|
|
return templatetext
|