Skip to content

Commit

Permalink
Merge pull request #3552 from BirkbeckCTP/b_1_5_0
Browse files Browse the repository at this point in the history
B 1 5 0
  • Loading branch information
mauromsl authored May 17, 2023
2 parents d2b5ea7 + de890c1 commit 125328d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ ifdef VERBOSE
_VERBOSE=--verbose
endif

# Email
JANEWAY_EMAIL_BACKEND=''
JANEWAY_EMAIL_HOST=''
JANEWAY_EMAIL_PORT=''
JANEWAY_EMAIL_USE_TLS=0

ifdef DEBUG_SMTP
JANEWAY_EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
JANEWAY_EMAIL_HOST=janeway-debug-smtp
JANEWAY_EMAIL_PORT=1025
JANEWAY_EMAIL_USE_TLS=
endif

export DB_VENDOR
export DB_HOST
export DB_PORT
Expand All @@ -54,6 +67,12 @@ export DB_USER
export DB_PASSWORD
export JANEWAY_PORT
export PGADMIN_PORT

export JANEWAY_EMAIL_BACKEND
export JANEWAY_EMAIL_HOST
export JANEWAY_EMAIL_PORT
export JANEWAY_EMAIL_USE_TLS

SUFFIX ?= $(shell date +%s)
SUFFIX := ${SUFFIX}
DATE := `date +"%y-%m-%d"`
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ services:
image: dadarek/wait-for-dependencies
depends_on:
- "janeway-${DB_VENDOR}"
- janeway-debug-smtp
command: "janeway-${DB_VENDOR}:${DB_PORT}"

janeway-web:
Expand All @@ -82,6 +83,11 @@ services:
- PYTHONDONTWRITEBYTECODE=yes
- JANEWAY_SETTINGS_MODULE=core.dev_settings
- NOSE_INCLUDE_EXE=1
- JANEWAY_EMAIL_BACKEND
- JANEWAY_EMAIL_HOST
- JANEWAY_EMAIL_PORT
- JANEWAY_EMAIL_USE_TLS

depends_on:
- "start_dependencies"
cap_add:
Expand All @@ -94,3 +100,7 @@ services:
command: /bin/bash
volumes:
- ./db/janeway.sqlite:/var/lib/janeway.sqlite
janeway-debug-smtp:
# Use same python as janeway-web to reduce the number of images
image: python:3.8
entrypoint: python -u -m smtpd -c DebuggingServer -n 0.0.0.0:1025
5 changes: 4 additions & 1 deletion src/core/dev_settings.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# SECURITY WARNING: keep the secret key used in production secret!
# You should change this key before you go live!
import os
DEBUG = True
SECRET_KEY = 'uxprsdhk^gzd-r=_287byolxn)$k6tsd8_cepl^s^tms2w1qrv'

# This is the default redirect if no other sites are found.
DEFAULT_HOST = 'https://www.example.org'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_BACKEND = os.environ.get(
'JANEWAY_EMAIL_BACKEND', 'django.core.mail.backends.console.EmailBackend',
)

URL_CONFIG = 'path' # path or domain

Expand Down
14 changes: 8 additions & 6 deletions src/core/janeway_global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,14 @@ def filter(self, record):
LOGIN_REDIRECT_URL = '/'
LOGIN_URL = '/login/'

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = ''
EMAIL_PORT = ''
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = True
EMAIL_BACKEND = os.environ.get(
'JANEWAY_EMAIL_BACKEND', 'django.core.mail.backends.smtp.EmailBackend',
)
EMAIL_HOST = os.environ.get("JANEWAY_EMAIL_HOST", '')
EMAIL_PORT = os.environ.get("JANEWAY_EMAIL_PORT", '')
EMAIL_HOST_USER = os.environ.get("JANEWAY_EMAIL_HOST_USER", '')
EMAIL_HOST_PASSWORD = os.environ.get("JANEWAY_EMAIL_HOST_PASSWORD", '')
EMAIL_USE_TLS = os.environ.get("JANEWAY_EMAIL_USE_TLS", True)
DUMMY_EMAIL_DOMAIN = "@journal.com"

# Settings for use with Mailgun
Expand Down
21 changes: 17 additions & 4 deletions src/utils/notify_plugins/notify_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.core.mail.message import sanitize_address
from django.utils.encoding import force_str
from django.utils.html import strip_tags

Expand Down Expand Up @@ -53,9 +54,14 @@ def send_email(subject, to, html, journal, request, bcc=None, cc=None, attachmen
else:
full_from_string = from_email

# handle django 3.2 raising an exception during sanitization
# This call is ported from django 1.11
# handle django 3.2 raising an exception when invalid characters are found
# during sanitization (call ported from Django 1.11)
full_from_string = parseaddr(force_str(full_from_string))
# As per #3545, not all backends sanitize from string before .send()
full_from_string = sanitize_address(
full_from_string, settings.DEFAULT_CHARSET,
)


# if a replyto is passed to this function, use that.
if replyto:
Expand All @@ -74,15 +80,22 @@ def send_email(subject, to, html, journal, request, bcc=None, cc=None, attachmen
if reply_to and not isinstance(reply_to, (tuple, list)):
reply_to = [reply_to]

msg = EmailMultiAlternatives(subject, strip_tags(html), full_from_string, to, bcc=bcc, cc=cc, reply_to=reply_to)
kwargs = dict(
bcc=bcc,
cc=cc,
)
if reply_to:
# Avoid empty mailboxes for servers not compliant with RFC 5322
kwargs["reply_to"] = reply_to

msg = EmailMultiAlternatives(subject, strip_tags(html), full_from_string, to, **kwargs)
msg.attach_alternative(html, "text/html")

if request and request.FILES and request.FILES.getlist('attachment'):
for file in request.FILES.getlist('attachment'):
file.open()
msg.attach(file.name, file.read(), file.content_type)
file.close()

return msg.send()


Expand Down

0 comments on commit 125328d

Please sign in to comment.