Skip to content
Snippets Groups Projects
Verified Commit 6139fa7b authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Define own function for sending emails and use it

parent 0179365e
No related branches found
No related tags found
1 merge request!874Resolve "Provide base templates for e-mails generated by apps"
...@@ -4,9 +4,9 @@ from django.conf import settings ...@@ -4,9 +4,9 @@ from django.conf import settings
from celery import Celery from celery import Celery
from celery.signals import task_failure from celery.signals import task_failure
from templated_email import send_templated_mail
from .util.core_helpers import get_site_preferences from .util.core_helpers import get_site_preferences
from .util.email import send_email
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aleksis.core.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aleksis.core.settings")
...@@ -20,7 +20,7 @@ def task_failure_notifier( ...@@ -20,7 +20,7 @@ def task_failure_notifier(
sender=None, task_id=None, exception=None, args=None, traceback=None, **kwargs sender=None, task_id=None, exception=None, args=None, traceback=None, **kwargs
): ):
recipient_list = [e[1] for e in settings.ADMINS] recipient_list = [e[1] for e in settings.ADMINS]
send_templated_mail( send_email(
template_name="celery_failure", template_name="celery_failure",
from_email=get_site_preferences()["mail__address"], from_email=get_site_preferences()["mail__address"],
recipient_list=recipient_list, recipient_list=recipient_list,
......
...@@ -8,10 +8,10 @@ from django.utils.translation import gettext as _ ...@@ -8,10 +8,10 @@ from django.utils.translation import gettext as _
import reversion import reversion
from reversion import set_comment from reversion import set_comment
from templated_email import send_templated_mail
from .util.celery_progress import ProgressRecorder, recorded_task from .util.celery_progress import ProgressRecorder, recorded_task
from .util.core_helpers import get_site_preferences from .util.core_helpers import get_site_preferences
from .util.email import send_email
class SolveOption: class SolveOption:
...@@ -274,9 +274,8 @@ def send_emails_for_data_checks(): ...@@ -274,9 +274,8 @@ def send_emails_for_data_checks():
for group in get_site_preferences()["general__data_checks_recipient_groups"]: for group in get_site_preferences()["general__data_checks_recipient_groups"]:
recipient_list += [p.mail_sender for p in group.announcement_recipients if p.email] recipient_list += [p.mail_sender for p in group.announcement_recipients if p.email]
send_templated_mail( send_email(
template_name="data_checks", template_name="data_checks",
from_email=get_site_preferences()["mail__address"],
recipient_list=recipient_list, recipient_list=recipient_list,
context={"results": results_with_checks}, context={"results": results_with_checks},
) )
......
...@@ -45,7 +45,6 @@ from oauth2_provider.models import ( ...@@ -45,7 +45,6 @@ from oauth2_provider.models import (
) )
from phonenumber_field.modelfields import PhoneNumberField from phonenumber_field.modelfields import PhoneNumberField
from polymorphic.models import PolymorphicModel from polymorphic.models import PolymorphicModel
from templated_email import send_templated_mail
from aleksis.core.data_checks import BrokenDashboardWidgetDataCheck, DataCheck, DataCheckRegistry from aleksis.core.data_checks import BrokenDashboardWidgetDataCheck, DataCheck, DataCheckRegistry
...@@ -65,6 +64,7 @@ from .mixins import ( ...@@ -65,6 +64,7 @@ from .mixins import (
) )
from .tasks import send_notification from .tasks import send_notification
from .util.core_helpers import generate_random_code, get_site_preferences, now_tomorrow from .util.core_helpers import generate_random_code, get_site_preferences, now_tomorrow
from .util.email import send_email
from .util.model_helpers import ICONS from .util.model_helpers import ICONS
FIELD_CHOICES = ( FIELD_CHOICES = (
...@@ -386,7 +386,7 @@ class Person(ExtensibleModel): ...@@ -386,7 +386,7 @@ class Person(ExtensibleModel):
recipients = recipients or [ recipients = recipients or [
get_site_preferences()["account__person_change_notification_contact"] get_site_preferences()["account__person_change_notification_contact"]
] ]
send_templated_mail( send_email(
template_name="person_changed", template_name="person_changed",
from_email=self.mail_sender_via, from_email=self.mail_sender_via,
headers={ headers={
......
from typing import Any, Dict, List, Optional
from django.conf import settings
from templated_email import send_templated_mail
from aleksis.core.util.core_helpers import get_site_preferences, process_custom_context_processors
def send_email(
template_name: str,
recipient_list: List[str],
context: Dict[str, Any],
from_email: Optional[str] = None,
**kwargs,
):
"""Send templated email with data from context processors."""
processed_context = process_custom_context_processors(settings.PDF_CONTEXT_PROCESSORS)
processed_context.update(context)
if not from_email:
from_address = get_site_preferences()["mail__address"]
from_name = get_site_preferences()["general__title"]
from_email = f"{from_name} <{from_address}>"
return send_templated_mail(
template_name=template_name,
from_email=from_email,
recipient_list=recipient_list,
context=processed_context,
**kwargs,
)
...@@ -8,9 +8,8 @@ from django.template.loader import get_template ...@@ -8,9 +8,8 @@ from django.template.loader import get_template
from django.utils.functional import lazy from django.utils.functional import lazy
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from templated_email import send_templated_mail
from .core_helpers import lazy_preference from .core_helpers import lazy_preference
from .email import send_email
try: try:
from twilio.rest import Client as TwilioClient from twilio.rest import Client as TwilioClient
...@@ -35,9 +34,8 @@ def _send_notification_email(notification: "Notification", template: str = "noti ...@@ -35,9 +34,8 @@ def _send_notification_email(notification: "Notification", template: str = "noti
"notification": notification, "notification": notification,
"notification_user": notification.recipient.addressing_name, "notification_user": notification.recipient.addressing_name,
} }
send_templated_mail( send_email(
template_name=template, template_name=template,
from_email=lazy_preference("mail", "address"),
recipient_list=[notification.recipient.email], recipient_list=[notification.recipient.email],
context=context, context=context,
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment