From 775139b9ed76319cd926c78d4d3831a441193e7f Mon Sep 17 00:00:00 2001 From: erichhasl Date: Mon, 4 Dec 2017 20:59:47 +0100 Subject: [PATCH] auto responder for betriebe --- web_dev/sas_web/meingoethopia/mailutils.py | 33 ++++++++++++++++++++++ web_dev/sas_web/meingoethopia/views.py | 12 ++++++++ web_dev/sas_web/sas_web/settings.py | 14 +++++++++ 3 files changed, 59 insertions(+) create mode 100644 web_dev/sas_web/meingoethopia/mailutils.py diff --git a/web_dev/sas_web/meingoethopia/mailutils.py b/web_dev/sas_web/meingoethopia/mailutils.py new file mode 100644 index 0000000..c45a82f --- /dev/null +++ b/web_dev/sas_web/meingoethopia/mailutils.py @@ -0,0 +1,33 @@ +from django.core import mail +from django.core.mail import EmailMessage +from django.conf import settings +from django.core.mail.backends.smtp import EmailBackend + + +NOT_SENT, SENT, PARTLY_SENT = 0, 1, 2 + + +def send_from_arbeitsministerium(subject, content, recipients, reply_to=None): + return send(subject, content, settings.EMAIL_ARBEITSMINISTERIUM, recipients, + reply_to=reply_to, + auth_user=settings.EMAIL_ARBEITSMINISTERIUM_USER, + auth_password=settings.EMAIL_ARBEITSMINISTERIUM_PASSWORD) + + +def send(subject, content, sender, recipients, reply_to=None, + auth_user=None, auth_password=None): + failed, succeeded = False, False + if type(recipients) != list: + recipients = [recipients] + for recipient in set(recipients): + try: + mail.send_mail(subject, content, sender, recipients, + auth_user=auth_user, + auth_password=auth_password) + except Exception as e: + print("Error when sending mail:", e) + failed = True + else: + succeeded = True + return NOT_SENT if failed and not succeeded else SENT if not failed\ + and succeeded else PARTLY_SENT diff --git a/web_dev/sas_web/meingoethopia/views.py b/web_dev/sas_web/meingoethopia/views.py index 41d6f45..3257bd8 100644 --- a/web_dev/sas_web/meingoethopia/views.py +++ b/web_dev/sas_web/meingoethopia/views.py @@ -2,6 +2,7 @@ from django.shortcuts import render from django import forms from captcha.fields import CaptchaField from .models import Betrieb, Partei, PresidentCandidate, Question +from .mailutils import send_from_arbeitsministerium class BetriebForm(forms.Form): @@ -51,6 +52,10 @@ def betrieb_new(request): ip_address=get_client_ip(request), confirmed=False) betrieb.save() + send_from_arbeitsministerium( + 'Anmeldung des Betriebs: {}'.format(betrieb.name), + MAIL_CONTENT_BETRIEB.format(name=betrieb.manager), [betrieb.email] + ) return render_confirmation(request) else: form = BetriebForm() @@ -118,3 +123,10 @@ def get_client_ip(request): else: ip = request.META.get('REMOTE_ADDR') return ip + + +MAIL_CONTENT_BETRIEB = "Lieber {name},\nvielen Dank für die Anmeldung Deines" \ + " Betriebes. Die Informationsveranstaltungen beginnen erst im Februar, deshalb\n" \ + "wird sich das Arbeitsministerium dann erst mit Dir in Verbindung setzen.\n\n" \ + "Viele Grüße,\n" \ + "Organisation Goethopia" diff --git a/web_dev/sas_web/sas_web/settings.py b/web_dev/sas_web/sas_web/settings.py index 93a12aa..651b85a 100644 --- a/web_dev/sas_web/sas_web/settings.py +++ b/web_dev/sas_web/sas_web/settings.py @@ -13,6 +13,8 @@ https://docs.djangoproject.com/en/1.8/ref/settings/ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os +deployed = '1' == os.environ.get('DJANGO_DEPLOY', '0') + BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -132,3 +134,15 @@ STATICFILES_DIRS = [ STATIC_ROOT = os.environ.get('DJANGO_STATIC_ROOT', os.path.join((os.path.join(BASE_DIR, os.pardir)), "static")) + +# Email setup + +EMAIL_HOST = os.environ.get('EMAIL_HOST', 'localhost') +EMAIL_PORT = 587 if deployed else 25 +EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER', '') +EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD', '') +EMAIL_USE_TLS = True if deployed else False + +EMAIL_ARBEITSMINISTERIUM = os.environ.get('EMAIL_ARBEITSMINISTERIUM', '') +EMAIL_ARBEITSMINISTERIUM_USER = os.environ.get('EMAIL_ARBEITSMINISTERIUM', '') +EMAIL_ARBEITSMINISTERIUM_PASSWORD = os.environ.get('EMAIL_ARBEITSMINISTERIUM_PASSWORD', '')