konova/kspneo/utils/mailer.py

52 lines
1.5 KiB
Python
Raw Normal View History

2021-07-01 13:36:07 +02:00
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 09.11.20
"""
import logging
from django.core.mail import send_mail
from konova.sub_settings.django_settings import DEFAULT_FROM_EMAIL
logger = logging.getLogger(__name__)
class Mailer:
"""
A wrapper for the django internal mailing functionality
"""
from_mail = None
to_mail = []
fail_silently = False
# Optional. Can be changed using the constructor to authenticate on the smtp server using other credentials
auth_user = None
auth_password = None
def __init__(self, to_mail: list, from_mail: str = DEFAULT_FROM_EMAIL, auth_user: str = None, auth_password: str = None, fail_silently: bool = False):
# Make sure given to_mail parameter is a list
if isinstance(to_mail, str):
to_mail = [to_mail]
self.from_mail = from_mail
self.to_mail = to_mail
self.fail_silently = fail_silently
self.auth_user = auth_user
self.auth_password = auth_password
def send(self, subject: str, msg: str):
"""
Sends a mail with subject and message
"""
return send_mail(
subject=subject,
message=msg,
from_email=self.from_mail,
recipient_list=self.to_mail,
fail_silently=self.fail_silently,
auth_user=self.auth_user,
auth_password=self.auth_password
)