Add default alias name to custom domain

This commit is contained in:
Sibren Vasse 2020-05-03 16:05:34 +02:00
parent 4ca6b02047
commit 0e4799030d
6 changed files with 76 additions and 3 deletions

View File

@ -51,6 +51,26 @@
</form>
</div>
<hr>
<div>Default Alias name</div>
<div class="small-text">
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.
</div>
<div>
<form method="post">
<input type="hidden" name="form-name" value="set-name">
<div class="form-group">
<input class="form-control"
value="{{ custom_domain.name or "" }}"
name="alias-name"
placeholder="Alias name">
</div>
<button class="btn btn-primary">Save</a>
</form>
</div>
<hr>
<h3 class="mb-0">Delete Domain</h3>
<div class="small-text mb-3">Please note that this operation is irreversible.

View File

@ -358,7 +358,8 @@
<div class="d-flex">
<div class="flex-grow-1 mr-2">
<input id="alias-name-{{ alias.id }}"
value="{{ alias.name or '' }}" class="form-control" placeholder="Alias name">
value="{{ alias.name or '' }}" class="form-control"
placeholder="{{ alias.custom_domain.name or "Alias name" }}">
</div>

View File

@ -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)

View File

@ -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"

View File

@ -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

View File

@ -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 ###