From f08ed17bb288adb9507cfaa82ae9509fa1e521ab Mon Sep 17 00:00:00 2001 From: Benni Baermann Date: Wed, 6 Jan 2021 10:59:47 +0100 Subject: [PATCH] mails will be send to departments now --- evapp/settings.py | 6 +++-- evapp/templates/evapp/IT_mail.txt | 1 + evapp/templates/evapp/OFFICE_mail.txt | 1 + evapp/views.py | 37 +++++++++++++++++---------- 4 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 evapp/templates/evapp/IT_mail.txt create mode 100644 evapp/templates/evapp/OFFICE_mail.txt diff --git a/evapp/settings.py b/evapp/settings.py index 9853b80..d6661b9 100644 --- a/evapp/settings.py +++ b/evapp/settings.py @@ -1,8 +1,10 @@ -MAILS = {'IT': {'TEMPLATE': 'it_template.txt', +EVA_MAIL = 'benni.baermann@wikimedia.de' + +MAILS = {'IT': { 'MAIL': 'it@wikimedia.de', 'DATA': ['laptop', 'os',], }, - 'OFFICE': {'TEMPLATE': 'office_template', + 'OFFICE': { 'MAIL': 'office@wikimedia.de', 'DATA': ['transponder',], }, diff --git a/evapp/templates/evapp/IT_mail.txt b/evapp/templates/evapp/IT_mail.txt new file mode 100644 index 0000000..8a51db6 --- /dev/null +++ b/evapp/templates/evapp/IT_mail.txt @@ -0,0 +1 @@ +blurb diff --git a/evapp/templates/evapp/OFFICE_mail.txt b/evapp/templates/evapp/OFFICE_mail.txt new file mode 100644 index 0000000..d0ffab5 --- /dev/null +++ b/evapp/templates/evapp/OFFICE_mail.txt @@ -0,0 +1 @@ +blarb diff --git a/evapp/views.py b/evapp/views.py index eb01859..07d5a30 100644 --- a/evapp/views.py +++ b/evapp/views.py @@ -1,28 +1,39 @@ +from smtplib import SMTPException + from django.views.generic.edit import CreateView from django.urls import reverse from django.http import HttpResponse +from django.core.mail import send_mail, BadHeaderError +from django.template.loader import get_template from .models import Employee from .forms import EmployeeForm -from .settings import MAILS +from .settings import MAILS, EVA_MAIL def success(request): return HttpResponse("gut gemacht!") -def send_mail(department): +def send_mail_to_department(department): 'send a mail to the given department with the nececcary notifications' - print(f'send mail to department {department}') + print(f'send mail to department {department}...') + + context = {} + try: + mail_template = get_template(f'evapp/{department}_mail.txt') + send_mail( + 'EVA: Neuzugang', + mail_template.render(context), + EVA_MAIL, + [MAILS[department]['MAIL']], + fail_silently=False) + except BadHeaderError: + # modell.delete() + return HttpResponse('Invalid header found. Data not saved!') + except SMTPException: + # modell.delete() + return HttpResponse('Error in sending mails (propably wrong adress?). Data not saved!') - # context = { 'data': data } - # try: - # mail_template = get_template('input/it_mail.txt') - # send_mail( - # 'EVA: Neuzugang', - # mail_template.render(context), - # IF_EMAIL, - # [data['email']], - # fail_silently=False) class EvaFormView(CreateView): model = Employee @@ -33,6 +44,6 @@ class EvaFormView(CreateView): def form_valid(self, form): for dep in MAILS: - send_mail(dep) + send_mail_to_department(dep) return super().form_valid(form)