On 10/31/18 5:57 AM, Thomas G wrote:
Hi,
https://gitlab.com/mailman/hyperkitty/blob/master/hyperkitty/lib/incoming.py...
I changed "sender" for this; # Sender try: from_str = header_to_unicode(message['From']) from_name, from_email = parseaddr(from_str) from_name = unidecode.unidecode(from_name).strip()
What is unidecode?
except (UnicodeDecodeError, UnicodeEncodeError): raise (ValueError("Non-ascii sender address", message)) try: sender_address = from_email.encode('ascii').decode("ascii").strip() except (UnicodeDecodeError, UnicodeEncodeError): if from_name: sender_address = re.sub("[^a-z0-9]", "", from_name.lower()) if not sender_address: sender_address = "unknown@unknown.com" else: sender_address = "unknown@unknown.com" print("Non-ascii sender addre -- Sender address replaced by ",
sender_address)
I got this output (example): |Non-ascii sender address -- Sender address replaced by stephaneblipblopstlablipblopgmailcom It's not really perfect, I'm working to do something better, but basically it's works.
The original code intends to provide some value for the email address where parseaddr returns a null address.
You have an address, and you would do better to remove the non-ascii from the address than to try to make and address out of the sanitized real name.
Your issue is parseaddr() returns an email address with non-ascii.
I would suggest that instead of
try: sender_address = from_email.encode('ascii').decode("ascii").strip() except (UnicodeDecodeError, UnicodeEncodeError): if from_name: sender_address = re.sub("[^a-z0-9]", "", from_name.lower()) if not sender_address: sender_address = "unknown@unknown.com" else: sender_address = "unknown@unknown.com"
you do
try: sender_address = from_email.encode('ascii').decode("ascii").strip() except (UnicodeDecodeError, UnicodeEncodeError): sender_address = from_email.encode('ascii', errors='replace').decode("ascii").strip()
I think that there is a issue in the original code: Because : "" raise ValueError("Non-ascii sender address", message) ""
The second part will be never call:
It will if there is no exception.
if not sender_address: if from_name: sender_address = re.sub("[^a-z0-9]", "", from_name.lower()) if not sender_address: sender_address = "unknown" sender_address = "{}@example.com".format(sender_address) else: sender_address = "unknown@example.com"
If you bypass : "" raise ValueError("Non-ascii sender address", message) "", by a simple print(), you will get an error due to an unassigned variable (sender_address).
-- Mark Sapiro <mark@msapiro.net> The highway is for gamblers, San Francisco Bay Area, California better use your sense - B. Dylan