diff --git a/base/backends.py b/base/backends.py index 20569efa6..50d1111fc 100644 --- a/base/backends.py +++ b/base/backends.py @@ -39,7 +39,7 @@ class DefaultHorillaMailBackend(EmailBackend): if self.configuration else ssl_certfile or getattr(settings, "ssl_certfile", None) ) - self.mail_sent_from = self.dynamic_username + super().__init__( host=self.dynamic_host, port=self.dynamic_port, @@ -93,16 +93,24 @@ class DefaultHorillaMailBackend(EmailBackend): else getattr(settings, "EMAIL_HOST_USER", None) ) + @property + def dynamic_mail_sent_from(self): + return ( + self.configuration.from_email + if self.configuration + else getattr(settings, "DEFAULT_FROM_EMAIL", None) + ) + @property def dynamic_display_name(self): return self.configuration.display_name if self.configuration else None @property - def dynamic_username_with_display_name(self): + def dynamic_from_email_with_display_name(self): return ( - f"{self.dynamic_display_name} <{self.dynamic_username}>" + f"{self.dynamic_display_name} <{self.dynamic_mail_sent_from}>" if self.dynamic_display_name - else self.dynamic_username + else self.dynamic_mail_sent_from ) @property @@ -167,7 +175,7 @@ class ConfiguredEmailBackend(BACKEND_CLASS): for message in email_messages: email_log = EmailLog( subject=message.subject, - from_email=self.mail_sent_from, + from_email=self.dynamic_from_email_with_display_name, to=message.to, body=message.body, status="sent" if response else "failed", @@ -177,9 +185,10 @@ class ConfiguredEmailBackend(BACKEND_CLASS): if EMAIL_BACKEND != default: - from_mail = getattr(settings, "EMAIL_HOST_USER", "example@gmail.com") + from_mail = getattr(settings, "DEFAULT_FROM_EMAIL", "example@gmail.com") + username = getattr(settings, "EMAIL_HOST_USER", "example@gmail.com") ConfiguredEmailBackend.dynamic_username = from_mail - ConfiguredEmailBackend.dynamic_username_with_display_name = from_mail + ConfiguredEmailBackend.dynamic_from_email_with_display_name = from_mail __all__ = ["ConfiguredEmailBackend"] diff --git a/base/views.py b/base/views.py index 38d1bce92..a816ca9a2 100644 --- a/base/views.py +++ b/base/views.py @@ -8,6 +8,7 @@ import json import uuid from datetime import datetime, timedelta from urllib.parse import parse_qs, unquote, urlencode +from os import path from django import forms from django.apps import apps @@ -486,7 +487,7 @@ class HorillaPasswordResetView(PasswordResetView): opts = { "use_https": self.request.is_secure(), "token_generator": self.token_generator, - "from_email": email_backend.dynamic_username_with_display_name, + "from_email": email_backend.dynamic_from_email_with_display_name, "email_template_name": self.email_template_name, "subject_template_name": self.subject_template_name, "request": self.request, @@ -1198,14 +1199,14 @@ def mail_server_test_email(request): msg = EmailMultiAlternatives( subject, text_content, - email_backend.dynamic_username_with_display_name, + email_backend.dynamic_from_email_with_display_name, [email_to], connection=email_backend, ) msg.attach_alternative(html_content, "text/html") # Attach the image - image_path = settings.STATIC_ROOT + "/images/ui/horilla-logo.png" + image_path = path.join(settings.STATIC_ROOT, "images/ui/horilla-logo.png") with open(image_path, "rb") as img: msg_img = MIMEImage(img.read()) msg_img.add_header("Content-ID", "") diff --git a/employee/not_in_out_dashboard.py b/employee/not_in_out_dashboard.py index f99e99718..c5882d82c 100644 --- a/employee/not_in_out_dashboard.py +++ b/employee/not_in_out_dashboard.py @@ -127,7 +127,7 @@ def send_mail_to_employee(request): (file.name, file.read(), file.content_type) for file in other_attachments ] email_backend = ConfiguredEmailBackend() - host = email_backend.dynamic_username + host = email_backend.dynamic_from_email_with_display_name if employee_id: employee_obj = Employee.objects.filter(id=employee_id) diff --git a/helpdesk/threading.py b/helpdesk/threading.py index d07b65f17..fe550f55c 100644 --- a/helpdesk/threading.py +++ b/helpdesk/threading.py @@ -62,7 +62,7 @@ class TicketSendThread(Thread): email = EmailMessage( subject, html_message, - email_backend.dynamic_username_with_display_name, + email_backend.dynamic_from_email_with_display_name, [recipient.email], ) email.content_subtype = "html" @@ -159,7 +159,7 @@ class AddAssigneeThread(Thread): email = EmailMessage( subject, html_message, - email_backend.dynamic_username_with_display_name, + email_backend.dynamic_from_email_with_display_name, [recipient.email], ) email.content_subtype = "html" @@ -210,7 +210,7 @@ class RemoveAssigneeThread(Thread): email = EmailMessage( subject, html_message, - email_backend.dynamic_username_with_display_name, + email_backend.dynamic_from_email_with_display_name, [recipient.email], ) email.content_subtype = "html" diff --git a/horilla_automations/signals.py b/horilla_automations/signals.py index 78cd44e47..0d418de70 100644 --- a/horilla_automations/signals.py +++ b/horilla_automations/signals.py @@ -348,7 +348,7 @@ def send_mail(request, automation, instance): to = tos[:1] cc = tos[1:] email_backend = ConfiguredEmailBackend() - host = email_backend.dynamic_username + host = email_backend.dynamic_from_email_with_display_name if mail_to_instance and request: attachments = [] try: diff --git a/leave/threading.py b/leave/threading.py index 56f303801..8433c92da 100644 --- a/leave/threading.py +++ b/leave/threading.py @@ -41,7 +41,7 @@ class LeaveMailSendThread(Thread): email = EmailMessage( subject, html_message, - email_backend.dynamic_username_with_display_name, + email_backend.dynamic_from_email_with_display_name, [recipient.email], ) email.content_subtype = "html" diff --git a/onboarding/views.py b/onboarding/views.py index a4b4e7b29..f961897d5 100644 --- a/onboarding/views.py +++ b/onboarding/views.py @@ -704,7 +704,7 @@ def email_send(request): email = EmailMessage( f"Hello {candidate.name}, Congratulations on your selection!", html_message, - email_backend.dynamic_username_with_display_name, + email_backend.dynamic_from_email_with_display_name, [candidate.email], ) email.content_subtype = "html" @@ -1627,7 +1627,7 @@ def onboarding_send_mail(request, candidate_id): res = send_mail( subject, body, - email_backend.dynamic_username_with_display_name, + email_backend.dynamic_from_email_with_display_name, [candidate_mail], fail_silently=False, ) diff --git a/payroll/threadings/mail.py b/payroll/threadings/mail.py index b65a04adb..b40768db3 100644 --- a/payroll/threadings/mail.py +++ b/payroll/threadings/mail.py @@ -57,7 +57,7 @@ class MailSendThread(Thread): email = EmailMessage( f"Hello, {record['instances'][0].get_name()} Your Payslips is Ready!", html_message, - email_backend.dynamic_username_with_display_name, + email_backend.dynamic_from_email_with_display_name, [employee.get_mail()], # reply_to=["another@example.com"], # headers={"Message-ID": "foo"}, diff --git a/payroll/views/component_views.py b/payroll/views/component_views.py index 2a059cea0..ed192f12d 100644 --- a/payroll/views/component_views.py +++ b/payroll/views/component_views.py @@ -974,8 +974,8 @@ def send_slip(request): payslip_ids = request.GET.getlist("id") payslips = Payslip.objects.filter(id__in=payslip_ids) if not getattr( - email_backend, "dynamic_username_with_display_name", None - ) or not len(email_backend.dynamic_username_with_display_name): + email_backend, "dynamic_from_email_with_display_name", None + ) or not len(email_backend.dynamic_from_email_with_display_name): messages.error(request, "Email server is not configured") if view: return HttpResponse("") diff --git a/recruitment/views.py b/recruitment/views.py index b123cb80b..ef5a3a30b 100644 --- a/recruitment/views.py +++ b/recruitment/views.py @@ -1262,7 +1262,7 @@ def send_acknowledgement(request): bdy = request.POST.get("body") email_backend = ConfiguredEmailBackend() res = send_mail( - subject, bdy, email_backend.dynamic_username, [send_to], fail_silently=False + subject, bdy, email_backend.dynamic_from_email_with_display_name, [send_to], fail_silently=False ) if res == 1: return HttpResponse( diff --git a/recruitment/views/views.py b/recruitment/views/views.py index bdf2cb4c0..27871c277 100644 --- a/recruitment/views/views.py +++ b/recruitment/views/views.py @@ -1925,7 +1925,7 @@ def send_acknowledgement(request): (file.name, file.read(), file.content_type) for file in other_attachments ] email_backend = ConfiguredEmailBackend() - host = email_backend.dynamic_username + host = email_backend.dynamic_from_email_with_display_name if candidate_id: candidate_obj = Candidate.objects.filter(id=candidate_id) else: