Skip to content

Commit

Permalink
Add support for space as separator of multiple email addresses
Browse files Browse the repository at this point in the history
Also fixes edge cases where string ended with a separator followed by 
whitespace
  • Loading branch information
lfdebrux committed Sep 5, 2018
1 parent fbfc11e commit 0df34aa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dmutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .flask_init import init_app, init_manager


__version__ = '43.3.0'
__version__ = '43.4.0'
7 changes: 5 additions & 2 deletions dmutils/email/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

# List of characters that could be used to separate email addresses,
# in decreasing precedence order.
EMAIL_ADDRESS_SEPARATORS = [";", ",", "/"]
# Space (" ") should almost always be at the end of this list, otherwise
# it can cause weird issues when there are separators surrounded by space.
EMAIL_ADDRESS_SEPARATORS = [";", ",", "/", " "]

# Largely copied from https://github.com/alphagov/notifications-utils/blob/\
# 67889886ec1476136d12e7f32787a7dbd0574cc2/notifications_utils/recipients.py
Expand Down Expand Up @@ -101,6 +103,7 @@ def get_email_addresses(string, separators=EMAIL_ADDRESS_SEPARATORS):
addresses = string.split(sep)
break # earlier separators take precedence

addresses = [s.strip() for s in addresses if s]
addresses = (s.strip() for s in addresses)
addresses = [s for s in addresses if s] # remove empty strings that can be left in by the algorithm

return addresses
6 changes: 5 additions & 1 deletion tests/email/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
["bob@blob"]),
("bob@blob.com;bob.blob@job..com",
["bob@blob.com", "bob.blob@job..com"]),
("mary.shelley@example.email; ada.lovelace@email.example; ",
["mary.shelley@example.email", "ada.lovelace@email.example"]),
("joan.d'arc@example.email dorothy.vaughn@email.example ",
["joan.d'arc@example.email", "dorothy.vaughn@email.example"]),
("Please send emails to bob@blob.com",
["Please send emails to bob@blob.com"]),
["Please", "send", "emails", "to", "bob@blob.com"]),
)
)
def test_get_email_addresses_returns_list_of_valid_email_addresses(multiple_email_addresses, expected):
Expand Down

0 comments on commit 0df34aa

Please sign in to comment.