mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-20 06:08:35 +02:00
@@ -5,7 +5,7 @@ from __future__ import unicode_literals
|
||||
import email
|
||||
from email.header import Header
|
||||
from email.mime.text import MIMEText
|
||||
from email.utils import make_msgid, formatdate, parseaddr
|
||||
from email.utils import make_msgid, formataddr, formatdate, getaddresses
|
||||
import re
|
||||
import smtplib
|
||||
import time
|
||||
@@ -341,22 +341,18 @@ def decode(string, encodings=None, charset=None):
|
||||
def prepare_addresses(addresses, usage="header"):
|
||||
"""Prepare addresses before using them
|
||||
|
||||
FIXME: We need a real address parser here! If a name contains a
|
||||
separator, it creates two wrong addresses.
|
||||
|
||||
:param list addresses: a list of addresses
|
||||
:param string usage: how those addresses are going to be used
|
||||
:return: a string or a list depending on usage value
|
||||
"""
|
||||
result = []
|
||||
for address in re.split('[,;]', addresses):
|
||||
for name, address in getaddresses(addresses):
|
||||
if not address:
|
||||
continue
|
||||
name, addr = parseaddr(address)
|
||||
if name and usage == "header":
|
||||
result.append("%s <%s>" % (Header(name, 'utf8'), addr))
|
||||
result.append(formataddr((name, address)))
|
||||
else:
|
||||
result.append(addr)
|
||||
result.append(address)
|
||||
if usage == "header":
|
||||
return ",".join(result)
|
||||
return result
|
||||
|
||||
@@ -9,7 +9,8 @@ import os
|
||||
from django.test import SimpleTestCase
|
||||
from django.utils.encoding import smart_bytes, smart_text
|
||||
|
||||
from ..email_utils import Email, split_address, split_local_part
|
||||
from ..email_utils import (
|
||||
Email, prepare_addresses, split_address, split_local_part)
|
||||
|
||||
SAMPLES_DIR = os.path.realpath(
|
||||
os.path.join(os.path.dirname(__file__), "sample_messages"))
|
||||
@@ -153,3 +154,33 @@ class EmailAddressParserTests(SimpleTestCase):
|
||||
expected_output = ("mailer-daemon", None)
|
||||
output = split_local_part(local_part, "-")
|
||||
self.assertEqual(output, expected_output)
|
||||
|
||||
def test_prepare_addresses(self):
|
||||
"""Check a list of e-mail addresses is sepearted correctly."""
|
||||
# value is an array with one long string not 3 sepearte values.
|
||||
value = [
|
||||
"\"Doe, John\" <doe.john@sub.example.com>;"
|
||||
"\"John Smith\" <john.smith@sub.example.com>,"
|
||||
"admin@sub.example.com"
|
||||
]
|
||||
expected_output = "\"Doe, John\" <doe.john@sub.example.com>,"\
|
||||
"John Smith <john.smith@sub.example.com>,"\
|
||||
"admin@sub.example.com"
|
||||
output = prepare_addresses(value)
|
||||
self.assertEqual(output, expected_output)
|
||||
|
||||
def test_prepare_addresses_usage_envelope(self):
|
||||
"""Check a list of e-mail addresses is sepearted correctly."""
|
||||
# value is an array with one long string not 3 sepearte values.
|
||||
value = [
|
||||
"\"Doe, John\" <doe.john@sub.example.com>;"
|
||||
"\"John Smith\" <john.smith@sub.example.com>,"
|
||||
"admin@sub.example.com"
|
||||
]
|
||||
expected_output = [
|
||||
"doe.john@sub.example.com",
|
||||
"john.smith@sub.example.com",
|
||||
"admin@sub.example.com",
|
||||
]
|
||||
output = prepare_addresses(value, usage="envelope")
|
||||
self.assertEqual(output, expected_output)
|
||||
|
||||
Reference in New Issue
Block a user