From 20499f81ae44770d8af67a0e985b929c6ef07baf Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sat, 29 May 2021 05:01:25 +0200 Subject: [PATCH] Base mail implementation --- localsettings.dist.py | 9 + mail/functions.py | 1 + mail/templatetags/__init__.py | 0 mail/templatetags/file_to_data.py | 29 ++ mail/views.py | 12 +- requirements.txt | 1 + templates/localauth/mail/verify.html | 36 +++ templates/localauth/mail/verify.txt | 13 + templates/mail/base.html | 402 +++++++++++++++++++++++++++ templates/mail/base.txt | 2 + urlaubsauktion/settings.py | 1 + 11 files changed, 504 insertions(+), 2 deletions(-) create mode 100644 mail/functions.py create mode 100644 mail/templatetags/__init__.py create mode 100644 mail/templatetags/file_to_data.py create mode 100644 templates/localauth/mail/verify.html create mode 100644 templates/localauth/mail/verify.txt create mode 100644 templates/mail/base.html create mode 100644 templates/mail/base.txt diff --git a/localsettings.dist.py b/localsettings.dist.py index 437a856..7477f5a 100644 --- a/localsettings.dist.py +++ b/localsettings.dist.py @@ -14,6 +14,15 @@ DB_NAME = "postgresql_database" DB_USER = "postgresql_username" DB_PASS = "postgresql_password" +# Mail server settings + +EMAIL_HOST = "mail.server" +EMAIL_PORT = None +EMAIL_HOST_USER = "mail_username" +EMAIL_HOST_PASSWORD = "mail_password" +EMAIL_USE_TLS = False +EMAIL_USE_SSL = True + # Email addresses of system administrators and managers ADMINS = [ diff --git a/mail/functions.py b/mail/functions.py new file mode 100644 index 0000000..c2c24d0 --- /dev/null +++ b/mail/functions.py @@ -0,0 +1 @@ +from django.core.mail import send_mail \ No newline at end of file diff --git a/mail/templatetags/__init__.py b/mail/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mail/templatetags/file_to_data.py b/mail/templatetags/file_to_data.py new file mode 100644 index 0000000..17cb227 --- /dev/null +++ b/mail/templatetags/file_to_data.py @@ -0,0 +1,29 @@ +from django import template +from django.templatetags.static import static + +from urllib.request import urlopen +from io import BytesIO + +import base64 + +import magic + +register = template.Library() + +def to_data(binary): + with BytesIO(binary) as bio: + mime = magic.from_buffer(bio.read(), mime=True) + + encoded = b"".join(base64.encodestring(binary).splitlines()).decode() + + return f"data:{ mime };base64,{ encoded }" + +@register.simple_tag +def url_to_data(url): + binary = urlopen(url).read() + return to_data(binary) + +@register.simple_tag +def static_to_data(path): + url = static(path) + return url_to_data(url) \ No newline at end of file diff --git a/mail/views.py b/mail/views.py index 91ea44a..f4fce86 100644 --- a/mail/views.py +++ b/mail/views.py @@ -1,3 +1,11 @@ -from django.shortcuts import render +from django.views.generic.base import ContextMixin +from django.template.loader import render_to_string -# Create your views here. +class MailView(ContextMixin): + def render_html(**kwargs): + context = self.get_context_data(**kwargs) + return render_to_string(self.html_template_name, context) + + def render_text(**kwargs): + context = self.get_context_data(**kwargs) + return render_to_string(self.text_template_name, context) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 39b375d..79c95f0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,4 +20,5 @@ django-mathfilters fast-luhn kumisms django-filtersignals +python-magic git+https://kumig.it/kumisystems/PyInvoice \ No newline at end of file diff --git a/templates/localauth/mail/verify.html b/templates/localauth/mail/verify.html new file mode 100644 index 0000000..153b282 --- /dev/null +++ b/templates/localauth/mail/verify.html @@ -0,0 +1,36 @@ +{% extends "mail/base.html" %} +{% load file_to_data %} + +{% block content %} + + + + + + + + +
+ +
+ + + + +
+
+

Bitte bestätige deine Registrierung, {{ first_name }}

+

Vielen Dank, dass du dich bei JourneyJoker registriert hast. Bitte bestätige deine E-Mail-Adresse, um dein Konto nutzen zu können!

+

Ja! Jetzt bestätigen!

+
+
+
+{% endblock %} + +{% block footer %} + + +

Du erhältst diese E-Mail, weil sich jemand mit deiner E-Mail-Adresse bei JourneyJoker angemeldet hat. Das warst nicht du? Dann kannst du diese Nachricht einfach ignorieren und wir bitten um Entschuldigung für die Störung!

+ + +{% endblock %} \ No newline at end of file diff --git a/templates/localauth/mail/verify.txt b/templates/localauth/mail/verify.txt new file mode 100644 index 0000000..d22e356 --- /dev/null +++ b/templates/localauth/mail/verify.txt @@ -0,0 +1,13 @@ +{% extends "mail/base.txt %} + +{% block content %} +Bitte bestätige deine Registrierung, {{ first_name }}! + +Vielen Dank, dass du dich bei JourneyJoker registriert hast. Bitte bestätige deine E-Mail-Adresse, um dein Konto nutzen zu können! + +Jetzt bestätigen: {{ confirmation_url }} + +--- + +Du erhältst diese E-Mail, weil sich jemand mit deiner E-Mail-Adresse bei JourneyJoker angemeldet hat. Das warst nicht du? Dann kannst du diese Nachricht einfach ignorieren und wir bitten um Entschuldigung für die Störung! +{% endblock %} \ No newline at end of file diff --git a/templates/mail/base.html b/templates/mail/base.html new file mode 100644 index 0000000..4f3c15b --- /dev/null +++ b/templates/mail/base.html @@ -0,0 +1,402 @@ +{% load file_to_data %} + + + + + + + + {{ title }} + + + + + + + + + + + + + + + +
+
+ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌  +
+ +
+ + \ No newline at end of file diff --git a/templates/mail/base.txt b/templates/mail/base.txt new file mode 100644 index 0000000..c6afe66 --- /dev/null +++ b/templates/mail/base.txt @@ -0,0 +1,2 @@ +{% block content %} +{% endblock %} \ No newline at end of file diff --git a/urlaubsauktion/settings.py b/urlaubsauktion/settings.py index 4cfd41f..731501c 100644 --- a/urlaubsauktion/settings.py +++ b/urlaubsauktion/settings.py @@ -26,6 +26,7 @@ INSTALLED_APPS = [ 'bootstrap4', 'django_countries', 'mathfilters', + 'mail', ] MIDDLEWARE = [