Skip to content

Commit

Permalink
fixup! ✨(emails) use mjml to generate html and text emails
Browse files Browse the repository at this point in the history
  • Loading branch information
wilbrdt committed Aug 21, 2024
1 parent 0b45355 commit a5383ea
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 deletions.
27 changes: 14 additions & 13 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ MORK_CELERY_RESULT_BACKEND=redis://redis:6379/0
MORK_CELERY_BROKER_TRANSPORT_OPTIONS={}
MORK_CELERY_TASK_DEFAULT_QUEUE=celery

# Emails
MORK_EMAIL_HOST=mailcatcher
MORK_EMAIL_HOST_USER=
MORK_EMAIL_HOST_PASSWORD=
MORK_EMAIL_PORT=1025
MORK_EMAIL_USE_TLS=False
[email protected]
MORK_EMAIL_RATE_LIMIT=100/m
MORK_EMAIL_MAX_RETRIES=3
MORK_EMAIL_SITE_NAME="France Université Numérique"
MORK_EMAIL_SITE_BASE_URL=https://fun-mooc.fr
MORK_EMAIL_SITE_LOGIN_URL=https://lms.fun-mooc.fr/login


# Python
PYTHONPATH=/app

Expand All @@ -48,16 +62,3 @@ MYSQL_PASSWORD=password
POSTGRES_DB=mork-db
POSTGRES_USER=fun
POSTGRES_PASSWORD=pass

# Emails
EMAIL_HOST=mailcatcher
EMAIL_HOST_USER=
EMAIL_HOST_PASSWORD=
EMAIL_PORT=1025
EMAIL_USE_TLS=False
[email protected]
EMAIL_RATE_LIMIT=100/m
EMAIL_MAX_RETRIES=3
EMAIL_SITE_NAME="France Université Numérique"
EMAIL_SITE_BASE_URL=https://fun-mooc.fr
EMAIL_SITE_LOGIN_URL=https://lms.fun-mooc.fr/login
5 changes: 4 additions & 1 deletion src/app/mork/celery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mork.celery.celery_app import app
from mork.conf import settings
from mork.database import MorkDB
from mork.exceptions import EmailAlreadySent, EmailSendError
from mork.exceptions import EmailAlreadySent, EmailSendError, EmailTemplateError
from mork.mail import send_email
from mork.models import EmailStatus

Expand Down Expand Up @@ -43,6 +43,9 @@ def send_email_task(self, email_address: str, username: str):
except EmailSendError as exc:
logger.exception(exc)
raise self.retry(exc=exc) from exc
except EmailTemplateError as exc:
logger.exception(exc)
raise exc

# Write flag that email was correctly sent to this user
mark_email_status(email_address)
Expand Down
4 changes: 4 additions & 0 deletions src/app/mork/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ class EmailAlreadySent(Exception):

class EmailSendError(Exception):
"""Raised when an error occurs when sending an email."""


class EmailTemplateError(Exception):
"""Raised when an error occurs on temlate configuration."""
9 changes: 8 additions & 1 deletion src/app/mork/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from jinja2 import Environment, FileSystemLoader

from mork.conf import settings
from mork.exceptions import EmailSendError
from mork.exceptions import EmailSendError, EmailTemplateError
from mork.templatetags.extra_tags import SVGStaticTag

logger = getLogger(__name__)
Expand All @@ -33,6 +33,13 @@ def render_template(template: str, context) -> str:

def send_email(email_address: str, username: str):
"""Initialize connection to SMTP and send a warning email."""
if (
not settings.EMAIL_SITE_NAME
or not settings.EMAIL_SITE_BASE_URL
or not settings.EMAIL_SITE_LOGIN_URL
):
raise EmailTemplateError("Email template misconfigured.")

template_vars = {
"title": "Votre compte va être supprimé dans 30 jours.",
"email": email_address,
Expand Down
19 changes: 18 additions & 1 deletion src/app/mork/tests/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from mork.exceptions import EmailSendError
from mork.exceptions import EmailSendError, EmailTemplateError
from mork.mail import render_template, send_email


Expand All @@ -28,6 +28,8 @@ def test_render_template():
assert template_vars["site"]["name"] in render_html
assert template_vars["site"]["url"] in render_html
assert template_vars["site"]["login_url"] in render_html
with open("test.html", "w") as f:
f.write(render_html)
assert 'src="data:' in render_html

render_text = render_template("warning_email.txt", template_vars)
Expand Down Expand Up @@ -68,3 +70,18 @@ def test_send_email_with_smtp_exception(monkeypatch):

with pytest.raises(EmailSendError, match="Failed sending an email"):
send_email(email_address=test_address, username=test_username)


def test_send_email_with_template_misconfiguration(monkeypatch):
"""Test the `send_email` function with an SMTP exception."""

mock_SMTP = MagicMock()
monkeypatch.setattr("mork.mail.smtplib.SMTP", mock_SMTP)

monkeypatch.setattr("mork.mail.settings.EMAIL_SITE_LOGIN_URL", "")

test_address = "[email protected]"
test_username = "JohnDoe"

with pytest.raises(EmailTemplateError, match="Email template misconfigured."):
send_email(email_address=test_address, username=test_username)
1 change: 1 addition & 0 deletions src/app/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dev = [
"black==24.8.0",
"build==1.2.1",
"factory_boy==3.3.0",
"flower==2.0.1",
"pytest==8.3.2",
"pytest-cov==5.0.0",
"pytest-httpx==0.30.0",
Expand Down

0 comments on commit a5383ea

Please sign in to comment.