Skip to content

Commit

Permalink
Introduce mail notification
Browse files Browse the repository at this point in the history
A mail will be sent when a new user registered (this implements #57)
Prepare mail sending by introducing required settings for dev and production, including entries in the matching secrets sample file (this finishes the implementation parts of #69)
  • Loading branch information
bhaettasch committed Mar 15, 2023
1 parent d66c6cc commit a9bad50
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dachor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,9 @@

LOGIN_URL = "/accounts/login/"
LOGIN_REDIRECT_URL = "/intern/"

# Emails
SEND_MAILS = True
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ADMIN_MAILS = ['[email protected]']
DEFAULT_FROM_EMAIL = '[email protected]'
18 changes: 18 additions & 0 deletions dachor/settings_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,21 @@
}

# TODO: caching

### EMAILS ###
SEND_MAILS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

# Addresses of all admins of this instance that will receive mail notifications
ADMIN_MAILS = secrets.MAIL_ADMINS

# Address to send from
SERVER_EMAIL = secrets.MAIL_ADDRESS
DEFAULT_FROM_EMAIL = SERVER_EMAIL

# SMTP Server Details
EMAIL_HOST = secrets.MAIL_HOST
EMAIL_PORT = secrets.MAIL_PORT
EMAIL_USE_TLS = secrets.MAIL_USE_TLS
EMAIL_HOST_USER = secrets.MAIL_USER
EMAIL_HOST_PASSWORD = secrets.MAIL_PASSWORD
11 changes: 11 additions & 0 deletions dachor/settings_secrets.py.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ HOSTS = []
DB_NAME = ''
DB_USER = ''
DB_PASSWORD = ''

# Addresses of all admins of this instance that will receive mail notifications
MAIL_ADMINS = []
# Address to send from
MAIL_SERVER_ADDRESS = ''
# SMTP Server Details
MAIL_HOST = ''
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_HOST_USER = ''
MAIL_HOST_PASSWORD = ''
21 changes: 21 additions & 0 deletions dachor_internal/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import datetime

from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.mail import EmailMessage
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.urls import reverse_lazy


def validate_birthday(value):
Expand Down Expand Up @@ -77,3 +80,21 @@ def save_user_profile(sender, instance, **kwargs):
profile.save()
except Profile.DoesNotExist:
pass


@receiver(post_save, sender=User)
def user_saved_handler(sender, instance: User, created, **kwargs):
"""
React to user creation by sending a notification mail to the instance admins
"""
if created and settings.SEND_MAILS:
host = 'https://' + settings.ALLOWED_HOSTS[0] if len(settings.ALLOWED_HOSTS) > 0 else 'http://127.0.0.1:8000'
url = f"{host}{reverse_lazy('admin:auth_user_change', kwargs={'object_id': instance.pk})}"

mail = EmailMessage(
f"[DA!CHOR Webseite] Neuer Benutzer '{instance.username}' angelegt",
f"Ein neuer Nutzer {instance.first_name} {instance.last_name} ({instance.username}) hat sich registriert.\n\nFreigeben: {url}",
settings.DEFAULT_FROM_EMAIL,
settings.ADMIN_MAILS
)
mail.send(fail_silently=True)

0 comments on commit a9bad50

Please sign in to comment.