mirror of
https://github.com/simple-login/app.git
synced 2024-11-10 09:13:45 +08:00
add Contact.new_addr()
This commit is contained in:
parent
b01533e9ac
commit
afceabeef5
3 changed files with 63 additions and 19 deletions
|
@ -381,25 +381,6 @@ def get_orig_message_from_spamassassin_report(msg: Message) -> Message:
|
|||
return part
|
||||
|
||||
|
||||
def new_addr(old_addr, new_email, user: User) -> str:
|
||||
"""replace First Last <first@example.com> by
|
||||
first@example.com by SimpleLogin <new_email>
|
||||
|
||||
`new_email` is a special reply address
|
||||
"""
|
||||
name, old_email = parseaddr(old_addr)
|
||||
if user.use_via_format_for_sender:
|
||||
new_name = f"{old_email} via SimpleLogin"
|
||||
else:
|
||||
name = name or ""
|
||||
new_name = (
|
||||
name + (" - " if name else "") + old_email.replace("@", " at ")
|
||||
).strip()
|
||||
|
||||
new_addr = formataddr((new_name, new_email)).strip()
|
||||
return new_addr.strip()
|
||||
|
||||
|
||||
def get_addrs_from_header(msg: Message, header) -> [str]:
|
||||
"""Get all addresses contained in `header`
|
||||
Used for To or CC header.
|
||||
|
|
|
@ -749,6 +749,7 @@ class Contact(db.Model, ModelMixin):
|
|||
is_cc = db.Column(db.Boolean, nullable=False, default=False, server_default="0")
|
||||
|
||||
alias = db.relationship(Alias, backref="contacts")
|
||||
user = db.relationship(User)
|
||||
|
||||
def website_send_to(self):
|
||||
"""return the email address with name.
|
||||
|
@ -785,6 +786,27 @@ class Contact(db.Model, ModelMixin):
|
|||
# cannot use formataddr here as this field is for email client, not for MTA
|
||||
return f'"{name}" <{self.reply_email}>'
|
||||
|
||||
def new_addr(self):
|
||||
"""
|
||||
Replace original email by reply_email. 2 possible formats:
|
||||
- first@example.com by SimpleLogin <reply_email> OR
|
||||
- First Last - first at example.com <reply_email>
|
||||
And return new address with RFC 2047 format
|
||||
|
||||
`new_email` is a special reply address
|
||||
"""
|
||||
user = self.user
|
||||
if user.use_via_format_for_sender:
|
||||
new_name = f"{self.website_email} via SimpleLogin"
|
||||
else:
|
||||
name = self.name or ""
|
||||
new_name = (
|
||||
name + (" - " if name else "") + self.website_email.replace("@", " at ")
|
||||
).strip()
|
||||
|
||||
new_addr = formataddr((new_name, self.reply_email)).strip()
|
||||
return new_addr.strip()
|
||||
|
||||
def last_reply(self) -> "EmailLog":
|
||||
"""return the most recent reply"""
|
||||
return (
|
||||
|
|
|
@ -4,6 +4,7 @@ import arrow
|
|||
import pytest
|
||||
|
||||
from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN
|
||||
from app.email_utils import parseaddr_unicode
|
||||
from app.extensions import db
|
||||
from app.models import generate_email, User, Alias, Contact
|
||||
|
||||
|
@ -93,3 +94,43 @@ def test_website_send_to(flask_client):
|
|||
c1.name = None
|
||||
c1.website_from = "=?UTF-8?B?TmjGoW4gTmd1eeG7hW4=?= <abcd@example.com>"
|
||||
assert c1.website_send_to() == '"Nhơn Nguyễn | abcd at example.com" <rep@SL>'
|
||||
|
||||
|
||||
def test_new_addr(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
db.session.commit()
|
||||
|
||||
# use_via_format_for_sender is by default
|
||||
c1 = Contact.create(
|
||||
user_id=user.id,
|
||||
alias_id=alias.id,
|
||||
website_email="abcd@example.com",
|
||||
reply_email="rep@SL",
|
||||
name="First Last",
|
||||
)
|
||||
db.session.commit()
|
||||
assert c1.new_addr() == '"abcd@example.com via SimpleLogin" <rep@SL>'
|
||||
|
||||
# use_via_format_for_sender = False
|
||||
user.use_via_format_for_sender = False
|
||||
db.session.commit()
|
||||
assert c1.new_addr() == '"First Last - abcd at example.com" <rep@SL>'
|
||||
|
||||
# unicode name
|
||||
c1.name = "Nhơn Nguyễn"
|
||||
db.session.commit()
|
||||
assert (
|
||||
c1.new_addr()
|
||||
== "=?utf-8?q?Nh=C6=A1n_Nguy=E1=BB=85n_-_abcd_at_example=2Ecom?= <rep@SL>"
|
||||
)
|
||||
|
||||
# sanity check for parseaddr_unicode
|
||||
assert parseaddr_unicode(c1.new_addr()) == (
|
||||
"Nhơn Nguyễn - abcd at example.com",
|
||||
"rep@sl",
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue