From 0e4799030dbaa7f8e30a28fc5b3c29588004e099 Mon Sep 17 00:00:00 2001 From: Sibren Vasse Date: Sun, 3 May 2020 16:05:34 +0200 Subject: [PATCH] Add default alias name to custom domain --- .../dashboard/domain_detail/info.html | 20 +++++++++++++ app/dashboard/templates/dashboard/index.html | 3 +- app/dashboard/views/domain_detail.py | 11 +++++++ app/models.py | 5 ++++ email_handler.py | 11 +++++-- .../versions/2020_050315_ae94fe5c4e9f_.py | 29 +++++++++++++++++++ 6 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 migrations/versions/2020_050315_ae94fe5c4e9f_.py diff --git a/app/dashboard/templates/dashboard/domain_detail/info.html b/app/dashboard/templates/dashboard/domain_detail/info.html index 42b4e7fe..5eb8d6f0 100644 --- a/app/dashboard/templates/dashboard/domain_detail/info.html +++ b/app/dashboard/templates/dashboard/domain_detail/info.html @@ -51,6 +51,26 @@ +
+
Default Alias name
+
+ This name will be used as the default alias name when you send + or reply from an alias, unless overwritten by the alias specific name. +
+ +
+
+ +
+ +
+
+

Delete Domain

Please note that this operation is irreversible. diff --git a/app/dashboard/templates/dashboard/index.html b/app/dashboard/templates/dashboard/index.html index c7ab1007..c96158aa 100644 --- a/app/dashboard/templates/dashboard/index.html +++ b/app/dashboard/templates/dashboard/index.html @@ -358,7 +358,8 @@
+ value="{{ alias.name or '' }}" class="form-control" + placeholder="{{ alias.custom_domain.name or "Alias name" }}">
diff --git a/app/dashboard/views/domain_detail.py b/app/dashboard/views/domain_detail.py index 1d5319cb..5f1fae21 100644 --- a/app/dashboard/views/domain_detail.py +++ b/app/dashboard/views/domain_detail.py @@ -141,6 +141,17 @@ def domain_detail(custom_domain_id): return redirect( url_for("dashboard.domain_detail", custom_domain_id=custom_domain.id) ) + elif request.form.get("form-name") == "set-name": + custom_domain.name = request.form.get("alias-name") + db.session.commit() + flash( + f"Default alias name for Domain {custom_domain.domain} has been set", + "success", + ) + + return redirect( + url_for("dashboard.domain_detail", custom_domain_id=custom_domain.id) + ) elif request.form.get("form-name") == "delete": name = custom_domain.domain CustomDomain.delete(custom_domain_id) diff --git a/app/models.py b/app/models.py index 14e5f9ec..761b3a5f 100644 --- a/app/models.py +++ b/app/models.py @@ -592,6 +592,8 @@ class Alias(db.Model, ModelMixin): db.ForeignKey("custom_domain.id", ondelete="cascade"), nullable=True ) + custom_domain = db.relationship("CustomDomain", foreign_keys=[custom_domain_id]) + # To know whether an alias is created "on the fly", i.e. via the custom domain catch-all feature automatic_creation = db.Column( db.Boolean, nullable=False, default=False, server_default="0" @@ -1036,6 +1038,9 @@ class CustomDomain(db.Model, ModelMixin): user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=False) domain = db.Column(db.String(128), unique=True, nullable=False) + # default name to use when user replies/sends from alias + name = db.Column(db.String(128), nullable=True, default=None) + verified = db.Column(db.Boolean, nullable=False, default=False) dkim_verified = db.Column( db.Boolean, nullable=False, default=False, server_default="0" diff --git a/email_handler.py b/email_handler.py index 80aa80b2..89a38dae 100644 --- a/email_handler.py +++ b/email_handler.py @@ -518,11 +518,18 @@ def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> (bool, str delete_header(msg, "Received") # make the email comes from alias + from_header = alias.email + # add alias name from alias if alias.name: LOG.d("Put alias name in from header") from_header = formataddr((alias.name, alias.email)) - else: - from_header = alias.email + elif alias.custom_domain: + LOG.d("Put domain default alias name in from header") + + # add alias name from domain + if alias.custom_domain.name: + from_header = formataddr((alias.custom_domain.name, alias.email)) + add_or_replace_header(msg, "From", from_header) # some email providers like ProtonMail adds automatically the Reply-To field diff --git a/migrations/versions/2020_050315_ae94fe5c4e9f_.py b/migrations/versions/2020_050315_ae94fe5c4e9f_.py new file mode 100644 index 00000000..add9e567 --- /dev/null +++ b/migrations/versions/2020_050315_ae94fe5c4e9f_.py @@ -0,0 +1,29 @@ +"""empty message + +Revision ID: ae94fe5c4e9f +Revises: de1b457472e0 +Create Date: 2020-05-03 15:24:23.151311 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ae94fe5c4e9f' +down_revision = 'de1b457472e0' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('custom_domain', sa.Column('name', sa.String(length=128), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('custom_domain', 'name') + # ### end Alembic commands ###