2024-01-04 11:05:11 +05:30
|
|
|
"""
|
|
|
|
|
email_backend.py
|
|
|
|
|
|
|
|
|
|
This module is used to write email backends
|
|
|
|
|
"""
|
2024-03-06 20:56:22 +05:30
|
|
|
|
2024-01-04 11:05:11 +05:30
|
|
|
from django.core.mail.backends.smtp import EmailBackend
|
2024-03-07 11:57:28 +05:30
|
|
|
from base.models import EmailLog, DynamicEmailConfiguration
|
2024-01-04 11:05:11 +05:30
|
|
|
from horilla import settings
|
|
|
|
|
from base.thread_local_middleware import _thread_locals
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConfiguredEmailBackend(EmailBackend):
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
host=None,
|
|
|
|
|
port=None,
|
|
|
|
|
username=None,
|
|
|
|
|
password=None,
|
|
|
|
|
use_tls=None,
|
|
|
|
|
fail_silently=None,
|
|
|
|
|
use_ssl=None,
|
|
|
|
|
timeout=None,
|
|
|
|
|
ssl_keyfile=None,
|
|
|
|
|
ssl_certfile=None,
|
|
|
|
|
**kwargs
|
|
|
|
|
):
|
2024-03-07 11:57:28 +05:30
|
|
|
self.configuration = self.get_dynamic_email_config()
|
2024-01-04 11:05:11 +05:30
|
|
|
|
2024-03-07 11:57:28 +05:30
|
|
|
ssl_keyfile = (
|
|
|
|
|
getattr(self.configuration, "ssl_keyfile", None)
|
|
|
|
|
if self.configuration
|
|
|
|
|
else ssl_keyfile or getattr(settings, "ssl_keyfile", None)
|
|
|
|
|
)
|
|
|
|
|
ssl_certfile = (
|
|
|
|
|
getattr(self.configuration, "ssl_certfile", None)
|
|
|
|
|
if self.configuration
|
|
|
|
|
else ssl_keyfile or getattr(settings, "ssl_certfile", None)
|
|
|
|
|
)
|
|
|
|
|
self.mail_sent_from = self.dynamic_username
|
|
|
|
|
super(ConfiguredEmailBackend, self).__init__(
|
|
|
|
|
host=self.dynamic_host,
|
|
|
|
|
port=self.dynamic_port,
|
|
|
|
|
username=self.dynamic_username,
|
|
|
|
|
password=self.dynamic_password,
|
|
|
|
|
use_tls=self.dynamic_use_tls,
|
|
|
|
|
fail_silently=self.dynamic_fail_silently,
|
|
|
|
|
use_ssl=self.dynamic_use_ssl,
|
|
|
|
|
timeout=self.dynamic_timeout,
|
|
|
|
|
ssl_keyfile=ssl_keyfile,
|
|
|
|
|
ssl_certfile=ssl_certfile,
|
|
|
|
|
**kwargs
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_dynamic_email_config():
|
2024-01-04 11:05:11 +05:30
|
|
|
request = getattr(_thread_locals, "request", None)
|
2024-03-04 14:39:52 +05:30
|
|
|
company = None
|
|
|
|
|
if request and not request.user.is_anonymous:
|
|
|
|
|
company = request.user.employee_get.get_company()
|
2024-01-04 11:05:11 +05:30
|
|
|
configuration = DynamicEmailConfiguration.objects.filter(
|
2024-03-04 14:39:52 +05:30
|
|
|
company_id=company
|
2024-01-04 11:05:11 +05:30
|
|
|
).first()
|
2024-01-29 11:48:32 +05:30
|
|
|
if configuration is None:
|
|
|
|
|
configuration = DynamicEmailConfiguration.objects.filter(
|
2024-03-06 20:56:22 +05:30
|
|
|
is_primary=True
|
2024-01-29 11:48:32 +05:30
|
|
|
).first()
|
2024-03-07 11:57:28 +05:30
|
|
|
return configuration
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def dynamic_host(self):
|
|
|
|
|
return (
|
|
|
|
|
self.configuration.host
|
|
|
|
|
if self.configuration
|
|
|
|
|
else getattr(settings, "EMAIL_HOST", None)
|
2024-01-04 11:05:11 +05:30
|
|
|
)
|
2024-03-07 11:57:28 +05:30
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def dynamic_port(self):
|
|
|
|
|
return (
|
|
|
|
|
self.configuration.port
|
|
|
|
|
if self.configuration
|
|
|
|
|
else getattr(settings, "EMAIL_PORT", None)
|
2024-01-04 11:05:11 +05:30
|
|
|
)
|
2024-03-07 11:57:28 +05:30
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def dynamic_username(self):
|
|
|
|
|
return (
|
|
|
|
|
self.configuration.username
|
|
|
|
|
if self.configuration
|
|
|
|
|
else getattr(settings, "EMAIL_HOST_USER", None)
|
2024-01-04 11:05:11 +05:30
|
|
|
)
|
2024-03-07 11:57:28 +05:30
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def dynamic_password(self):
|
|
|
|
|
return (
|
|
|
|
|
self.configuration.password
|
|
|
|
|
if self.configuration
|
|
|
|
|
else getattr(settings, "EMAIL_HOST_PASSWORD", None)
|
2024-01-04 11:05:11 +05:30
|
|
|
)
|
2024-03-07 11:57:28 +05:30
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def dynamic_use_tls(self):
|
|
|
|
|
return (
|
|
|
|
|
self.configuration.use_tls
|
|
|
|
|
if self.configuration
|
|
|
|
|
else getattr(settings, "EMAIL_USE_TLS", None)
|
2024-01-04 11:05:11 +05:30
|
|
|
)
|
2024-03-07 11:57:28 +05:30
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def dynamic_fail_silently(self):
|
|
|
|
|
return (
|
|
|
|
|
self.configuration.fail_silently
|
|
|
|
|
if self.configuration
|
|
|
|
|
else getattr(settings, "EMAIL_FAIL_SILENTLY", True)
|
2024-01-04 11:05:11 +05:30
|
|
|
)
|
2024-03-07 11:57:28 +05:30
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def dynamic_use_ssl(self):
|
|
|
|
|
return (
|
|
|
|
|
self.configuration.use_ssl
|
|
|
|
|
if self.configuration
|
|
|
|
|
else getattr(settings, "EMAIL_USE_SSL", None)
|
2024-01-04 11:05:11 +05:30
|
|
|
)
|
2024-03-07 11:57:28 +05:30
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def dynamic_timeout(self):
|
|
|
|
|
return (
|
|
|
|
|
self.configuration.timeout
|
|
|
|
|
if self.configuration
|
|
|
|
|
else getattr(settings, "EMAIL_TIMEOUT", None)
|
2024-01-04 11:05:11 +05:30
|
|
|
)
|
|
|
|
|
|
2024-01-29 11:48:32 +05:30
|
|
|
def send_messages(self, email_messages):
|
|
|
|
|
response = super(ConfiguredEmailBackend, self).send_messages(email_messages)
|
|
|
|
|
# Save the email status in the EmailLog model
|
|
|
|
|
for message in email_messages:
|
|
|
|
|
email_log = EmailLog(
|
|
|
|
|
subject=message.subject,
|
|
|
|
|
from_email=self.mail_sent_from,
|
|
|
|
|
to=message.to,
|
|
|
|
|
body=message.body,
|
|
|
|
|
status="sent" if response else "failed",
|
|
|
|
|
)
|
|
|
|
|
email_log.save()
|
|
|
|
|
return response
|
|
|
|
|
|
2024-01-04 11:05:11 +05:30
|
|
|
|
|
|
|
|
__all__ = ["ConfiguredEmailBackend"]
|