|
| 1 | +# Validating Email Addresses |
| 2 | +Example of how to use in a script |
| 3 | + |
| 4 | +```python |
| 5 | + |
| 6 | +from dsg_lib.common_functions.email_validation import validate_email_address |
| 7 | + |
| 8 | +import pprint |
| 9 | +import time |
| 10 | + |
| 11 | + |
| 12 | +if __name__ == "__main__": |
| 13 | + |
| 14 | + # create a list of email addresses to check if valid |
| 15 | + email_addresses = [ |
| 16 | + "bob@devsetgo.com", |
| 17 | + "bob@devset.go", |
| 18 | + "foo@yahoo.com", |
| 19 | + "bob@gmail.com", |
| 20 | + "very fake@devsetgo.com", |
| 21 | + "jane.doe@example.com", |
| 22 | + "john_doe@example.co.uk", |
| 23 | + "user.name+tag+sorting@example.com", |
| 24 | + "x@example.com", # shortest possible email address |
| 25 | + "example-indeed@strange-example.com", |
| 26 | + "admin@mailserver1", # local domain name with no TLD |
| 27 | + "example@s.example", # see the list of Internet top-level domains |
| 28 | + '" "@example.org', # space between the quotes |
| 29 | + '"john..doe"@example.org', # quoted double dot |
| 30 | + "mailhost!username@example.org", # bangified host route used for uucp mailers |
| 31 | + "user%example.com@example.org", # percent sign in local part |
| 32 | + "user-@example.org", # valid due to the last character being an allowed character |
| 33 | + # Invalid email addresses |
| 34 | + "Abc.example.com", # no @ character |
| 35 | + "A@b@c@example.com", # only one @ is allowed outside quotation marks |
| 36 | + 'a"b(c)d,e:f;g<h>i[j\\k]l@example.com', # none of the special characters in this local part are allowed outside quotation marks |
| 37 | + 'just"not"right@example.com', # quoted strings must be dot separated or the only element making up the local-part |
| 38 | + 'this is"not\\allowed@example.com', # spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash |
| 39 | + 'this\\ still\\"not\\\\allowed@example.com', # even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes |
| 40 | + "1234567890123456789012345678901234567890123456789012345678901234+x@example.com", # local part is longer than 64 characters |
| 41 | + |
| 42 | + # Emails with empty local part |
| 43 | + "@example.com", # only valid if allow_empty_local is True |
| 44 | + |
| 45 | + # Emails with non-ASCII characters |
| 46 | + "üñîçøðé@example.com", # only valid if allow_smtputf8 is True |
| 47 | + "user@üñîçøðé.com", # only valid if allow_smtputf8 is True |
| 48 | + |
| 49 | + # Emails with quoted local part |
| 50 | + '"john.doe"@example.com', # only valid if allow_quoted_local is True |
| 51 | + '"john..doe"@example.com', # only valid if allow_quoted_local is True |
| 52 | + |
| 53 | + # Emails with display name |
| 54 | + 'John Doe <john@example.com>', # only valid if allow_display_name is True |
| 55 | + |
| 56 | + # Emails with domain literal |
| 57 | + 'user@[192.0.2.1]', # only valid if allow_domain_literal is True |
| 58 | + |
| 59 | + # Emails with long local part |
| 60 | + "a"*65 + "@example.com", # local part is longer than 64 characters |
| 61 | + |
| 62 | + # Emails with invalid characters |
| 63 | + "john doe@example.com", # space is not allowed |
| 64 | + "john@doe@example.com", # only one @ is allowed |
| 65 | + "john.doe@.com", # domain can't start with a dot |
| 66 | + "john.doe@example..com", # domain can't have two consecutive dots |
| 67 | + "test@google.com", |
| 68 | + ] |
| 69 | + |
| 70 | + # create a list of configurations |
| 71 | + configurations = [ |
| 72 | + {"check_deliverability": True, "test_environment": False, "allow_smtputf8": False, "allow_empty_local": False, "allow_quoted_local": False, "allow_display_name": False, "allow_domain_literal": False, "globally_deliverable": None, "timeout": 10, "dns_type": 'timeout'}, |
| 73 | + {"check_deliverability": False, "test_environment": True, "allow_smtputf8": True, "allow_empty_local": True, "allow_quoted_local": True, "allow_display_name": True, "allow_domain_literal": True, "globally_deliverable": None, "timeout": 5, "dns_type": 'dns'}, |
| 74 | + {"check_deliverability": True}, |
| 75 | + {"check_deliverability": False, "test_environment": False, "allow_smtputf8": True, "allow_empty_local": False, "allow_quoted_local": True, "allow_display_name": False, "allow_domain_literal": True, "globally_deliverable": None, "timeout": 15, "dns_type": 'timeout'}, |
| 76 | + {"check_deliverability": True, "test_environment": True, "allow_smtputf8": False, "allow_empty_local": True, "allow_quoted_local": False, "allow_display_name": True, "allow_domain_literal": False, "globally_deliverable": None, "timeout": 20, "dns_type": 'dns'}, |
| 77 | + {"check_deliverability": False, "test_environment": False, "allow_smtputf8": True, "allow_empty_local": True, "allow_quoted_local": True, "allow_display_name": True, "allow_domain_literal": True, "globally_deliverable": None, "timeout": 25, "dns_type": 'timeout'}, |
| 78 | + {"check_deliverability": True, "test_environment": True, "allow_smtputf8": False, "allow_empty_local": False, "allow_quoted_local": False, "allow_display_name": False, "allow_domain_literal": False, "globally_deliverable": None, "timeout": 30, "dns_type": 'dns'}, |
| 79 | + {"check_deliverability": False, "test_environment": True, "allow_smtputf8": True, "allow_empty_local": False, "allow_quoted_local": True, "allow_display_name": True, "allow_domain_literal": False, "globally_deliverable": None, "timeout": 35, "dns_type": 'timeout'}, |
| 80 | + {"check_deliverability": True, "test_environment": False, "allow_smtputf8": False, "allow_empty_local": True, "allow_quoted_local": True, "allow_display_name": False, "allow_domain_literal": True, "globally_deliverable": None, "timeout": 40, "dns_type": 'dns'}, |
| 81 | + {"check_deliverability": False, "test_environment": True, "allow_smtputf8": True, "allow_empty_local": False, "allow_quoted_local": False, "allow_display_name": True, "allow_domain_literal": True, "globally_deliverable": None, "timeout": 45, "dns_type": 'timeout'}, |
| 82 | + ] |
| 83 | + |
| 84 | + t0 = time.time() |
| 85 | + validity=[] |
| 86 | + |
| 87 | + for email in email_addresses: |
| 88 | + for config in configurations: |
| 89 | + |
| 90 | + res = validate_email_address(email, **config) |
| 91 | + validity.append(res) |
| 92 | + t1 = time.time() |
| 93 | + validity = sorted(validity, key=lambda x: x['email']) |
| 94 | + |
| 95 | + for v in validity: |
| 96 | + pprint.pprint(v, indent=4) |
| 97 | + |
| 98 | + print(f"Time taken: {t1 - t0:.2f}") |
| 99 | +``` |
0 commit comments