remail: Use email.utils.get_addresses()

Replace more historical homebrewn parsing

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
This commit is contained in:
Thomas Gleixner 2023-06-16 23:31:07 +02:00
parent 893b9510db
commit 88f8e48b13
2 changed files with 4 additions and 37 deletions

View file

@ -5,7 +5,7 @@
# Mail message related code
from email.utils import make_msgid, formatdate, parseaddr
from email.header import Header, decode_header
from email.header import Header
from email import message_from_string, message_from_bytes
from email.generator import Generator
from email.message import Message, EmailMessage
@ -152,37 +152,6 @@ def get_raw_email_addr(addr):
'''
return parseaddr(addr)[1]
re_compress_space = re.compile('\s+')
def decode_hdr(hdr):
'''
Decode a mail header with encoding
'''
elm = decode_header(hdr.strip())
res = ''
for txt, enc in elm:
# Groan ....
if enc:
res += ' ' + txt.decode(enc)
elif isinstance(txt, str):
res += ' ' + txt
else:
res += ' ' + txt.decode('ascii')
return re_compress_space.sub(' ', res).strip()
def decode_addrs(hdr):
'''
Decode mail addresses from a header and handle encondings
'''
addrs = []
if not hdr:
return addrs
parts = re_compress_space.sub(' ', hdr).split(',')
for p in parts:
addr = decode_hdr(p)
addrs.append(addr)
return addrs
re_noquote = re.compile('[a-zA-Z0-9_\- ]+')
def encode_addr(fulladdr):

View file

@ -6,15 +6,15 @@
from remail.mail import msg_set_header, msg_force_msg_id, send_mail
from remail.mail import msg_sanitize_incoming, msg_is_autoreply
from remail.mail import get_raw_email_addr, decode_addrs
from remail.mail import msg_from_string, sender_info
from remail.mail import get_raw_email_addr
from remail.smime import smime_crypt, RemailSmimeException
from remail.gpg import gpg_crypt, RemailGPGException
from remail.tracking import account_tracking
from remail.config import accounts_config, gpg_config, smime_config
from email.utils import make_msgid, formatdate
from email.utils import make_msgid, formatdate, getaddresses
from email.policy import EmailPolicy
from flufl.bounce import all_failures
@ -303,10 +303,8 @@ class maillist(object):
def get_destination(self, msg):
# Handle the case where someone put several addresses on To:
addrs = decode_addrs(msg['To'])
for addr in addrs:
to = get_raw_email_addr(addr)
for name, to in getaddresses(msg.get_all('To', [])):
dest = self.config.listaddrs.get_destination(to, self.config.admins,
self.config.subscribers)
if dest: