clean up mail code

This commit is contained in:
Oliver Zander 2025-10-17 12:06:23 +02:00
parent 5d107dab96
commit 76ba63002d
3 changed files with 40 additions and 28 deletions

View File

@ -2,7 +2,17 @@ from django.conf import settings
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template from django.template.loader import get_template
from django.utils.html import strip_tags from django.utils.html import strip_tags
from .models import TYPE_CHOICES
from input.models import TYPE_CHOICES
from .attachments import collect_attachment_paths, attach_files
__all__ = [
'collect_attachment_paths',
'attach_files',
'send_decision_mail',
'send_staff_decision_mail',
]
def _type_labels(choice: str): def _type_labels(choice: str):

View File

@ -4,11 +4,14 @@ import time
import urllib.request import urllib.request
import urllib.parse import urllib.parse
import mimetypes import mimetypes
from pathlib import Path from pathlib import Path
from typing import Iterable, List, Tuple from typing import Iterable, List, Tuple
from django.conf import settings from django.conf import settings
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
def _ensure_cache_dir() -> Path: def _ensure_cache_dir() -> Path:
""" """
Ensure that the cache directory for attachments exists. Ensure that the cache directory for attachments exists.

View File

@ -12,7 +12,8 @@ from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView from django.views.generic import TemplateView
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
from django.utils.html import strip_tags from django.utils.html import strip_tags
from input.mail_attachments import collect_attachment_paths, attach_files
from input.utils.mail import collect_attachment_paths, attach_files
from .forms import ( from .forms import (
BaseApplicationForm, BaseApplicationForm,
@ -263,42 +264,40 @@ class ApplicationView(FormView):
def send_mail(self, obj, data): def send_mail(self, obj, data):
# Prepare minimal mail context and send mails # Prepare minimal mail context and send mails
data['pk'] = obj.pk
data['url_prefix'] = settings.EMAIL_URL_PREFIX
data['type_label'] = self.type_info.label
context = {'data': data}
try:
# Mail to applicant
txt1 = get_template('input/ifg_volunteer_mail.txt').render(context)
html1 = get_template('input/ifg_volunteer_mail.html').render(context)
msg1 = EmailMultiAlternatives(
'Deine Förderanfrage bei Wikimedia Deutschland', txt1, settings.IF_EMAIL, [data['email']]
)
msg1.attach_alternative(html1, 'text/html')
applicant_files = collect_attachment_paths(kind='applicant', choice=self.type_code)
attach_files(msg1, applicant_files)
msg1.send()
type_label_html = self.type_info.label type_label_html = self.type_info.label
type_label_plain = strip_tags(type_label_html) type_label_plain = strip_tags(type_label_html)
# Mail to IF
txt2 = get_template('input/if_mail.txt').render(context)
html2 = get_template('input/if_mail.html').render(context)
applicant_name = self.get_recipient_name(obj, data)
msg2 = EmailMultiAlternatives(
f'Anfrage {type_label_plain} von {applicant_name}', txt2, settings.IF_EMAIL, [settings.IF_EMAIL]
)
msg2.attach_alternative(html2, 'text/html')
staff_files = collect_attachment_paths(kind='staff', choice=self.type_code)
attach_files(msg2, staff_files)
msg2.send()
data['pk'] = obj.pk
data['url_prefix'] = settings.EMAIL_URL_PREFIX
data['type_label'] = type_label_html
context = {'data': data}
applicant_name = self.get_recipient_name(obj, data)
applicant_subject = 'Deine Förderanfrage bei Wikimedia Deutschland'
staff_subject = f'Anfrage {type_label_plain} von {applicant_name}'
try:
self.send_email('applicant', 'ifg_volunteer_mail', applicant_subject, data['email'], context)
self.send_email('staff', 'if_mail', staff_subject, settings.IF_EMAIL, context)
except BadHeaderError: except BadHeaderError:
obj.delete() obj.delete()
return HttpResponse('Invalid header found. Data not saved!') return HttpResponse('Invalid header found. Data not saved!')
except SMTPException: except SMTPException:
obj.delete() obj.delete()
return HttpResponse('Error in sending mails (probably wrong adress?). Data not saved!') return HttpResponse('Error in sending mails (probably wrong address?). Data not saved!')
def send_email(self, kind, template_name, subject, recipient, context):
plain = get_template(f'input/{template_name}.txt').render(context)
html = get_template(f'input/{template_name}.html').render(context)
email = EmailMultiAlternatives(subject, plain, settings.IF_EMAIL, [recipient])
applicant_files = collect_attachment_paths(kind=kind, choice=self.type_code)
email.attach_alternative(html, 'text/html')
attach_files(email, applicant_files)
return email.send()
@staticmethod @staticmethod
def get_recipient_name(obj, data): def get_recipient_name(obj, data):