From 90fa4abf698a0e9ccefdc095654964aeaa11c647 Mon Sep 17 00:00:00 2001 From: Son Date: Mon, 24 Jan 2022 16:10:36 +0100 Subject: [PATCH] create a notification for a bounce email --- app/models.py | 17 +++++++++ email_handler.py | 16 +++++++++ templates/header.html | 5 +-- .../notification/bounce-forward-phase.html | 36 +++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 templates/notification/bounce-forward-phase.html diff --git a/app/models.py b/app/models.py index 641bc7f2..0f50667a 100644 --- a/app/models.py +++ b/app/models.py @@ -1,4 +1,5 @@ import enum +import os import random import uuid from email.utils import formataddr @@ -11,6 +12,7 @@ from email_validator import validate_email from flanker.addresslib import address from flask import url_for from flask_login import UserMixin +from jinja2 import FileSystemLoader, Environment from sqlalchemy import orm from sqlalchemy import text, desc, CheckConstraint, Index, Column from sqlalchemy.dialects.postgresql import TSVECTOR @@ -33,6 +35,7 @@ from app.config import ( ALIAS_RANDOM_SUFFIX_LENGTH, MAX_NB_SUBDOMAIN, MAX_NB_DIRECTORY, + ROOT_DIR, ) from app.db import Session from app.errors import ( @@ -2563,6 +2566,20 @@ class Notification(Base, ModelMixin): # whether user has marked the notification as read read = sa.Column(sa.Boolean, nullable=False, default=False) + @staticmethod + def render(template_name, **kwargs) -> str: + templates_dir = os.path.join(ROOT_DIR, "templates") + env = Environment(loader=FileSystemLoader(templates_dir)) + + template = env.get_template(template_name) + + return template.render( + URL=URL, + LANDING_PAGE_URL=LANDING_PAGE_URL, + YEAR=arrow.now().year, + **kwargs, + ) + class SLDomain(Base, ModelMixin): """SimpleLogin domains""" diff --git a/email_handler.py b/email_handler.py index b6cd48b6..d41e73c6 100644 --- a/email_handler.py +++ b/email_handler.py @@ -149,6 +149,7 @@ from app.models import ( MessageIDMatching, DeletedAlias, DomainDeletedAlias, + Notification, ) from app.pgp_utils import PGPException, sign_data_with_pgpy, sign_data from app.utils import sanitize_email @@ -1396,6 +1397,21 @@ def handle_bounce_forward_phase(msg: Message, email_log: EmailLog): ) disable_alias_link = f"{URL}/dashboard/unsubscribe/{alias.id}" block_sender_link = f"{URL}/dashboard/alias_contact_manager/{alias.id}?highlight_contact_id={contact.id}" + + Notification.create( + user_id=user.id, + title=f"Email from {contact.website_email} to {alias.email} cannot be delivered to {mailbox.email}", + message=Notification.render( + "notification/bounce-forward-phase.html", + alias=alias, + website_email=contact.website_email, + disable_alias_link=disable_alias_link, + refused_email_url=refused_email.get_url(), + mailbox_email=mailbox.email, + block_sender_link=block_sender_link, + ), + commit=True, + ) send_email_with_rate_control( user, ALERT_BOUNCE_EMAIL, diff --git a/templates/header.html b/templates/header.html index 3c6822cf..cc7f340d 100644 --- a/templates/header.html +++ b/templates/header.html @@ -34,8 +34,9 @@
Loading ...