Skip to content

Commit 18106ca

Browse files
committed
In the __main__ tool read options to validate_email from environment variables
1 parent 1b9e867 commit 18106ca

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ There are no significant changes to which email addresses are considered valid/i
1313
* Some other error messages have changed to not repeat the email address in the error message.
1414
* The library has been reorganized internally into smaller modules.
1515
* The tests have been reorganized and expanded. Deliverability tests now mostly use captured DNS responses so they can be run off-line.
16+
* The __main__ tool now reads options to validate_email from environment variables.
1617
* Type annotations have been added to the exported methods and the ValidatedEmail class and some internal methods.
1718

1819
Version 1.3.1 (January 21, 2023)

email_validator/__main__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
# invalid email addresses. When passing an email address on the command
1111
# line, if the email address is valid, information about it will be printed.
1212
# When using STDIN, no output will be given for valid email addresses.
13+
#
14+
# Keyword arguments to validate_email can be set in environment variables
15+
# of the same name but upprcase (see below).
1316

1417
import json
18+
import os
1519
import sys
1620

1721
from .validate_email import validate_email
@@ -22,20 +26,30 @@
2226
def main(dns_resolver=None):
2327
# The dns_resolver argument is for tests.
2428

29+
# Set options from environment variables.
30+
options = {}
31+
for varname in ('ALLOW_SMTPUTF8', 'ALLOW_QUOTED_LOCAL', 'GLOBALLY_DELIVERABLE',
32+
'CHECK_DELIVERABILITY', 'TEST_ENVIRONMENT'):
33+
if varname in os.environ:
34+
options[varname.lower()] = bool(os.environ[varname])
35+
for varname in ('DEFAULT_TIMEOUT',):
36+
if varname in os.environ:
37+
options[varname.lower()] = float(os.environ[varname])
38+
2539
if len(sys.argv) == 1:
2640
# Validate the email addresses pased line-by-line on STDIN.
2741
dns_resolver = dns_resolver or caching_resolver()
2842
for line in sys.stdin:
2943
email = line.strip()
3044
try:
31-
validate_email(email, dns_resolver=dns_resolver)
45+
validate_email(email, dns_resolver=dns_resolver, **options)
3246
except EmailNotValidError as e:
3347
print(f"{email} {e}")
3448
else:
3549
# Validate the email address passed on the command line.
3650
email = sys.argv[1]
3751
try:
38-
result = validate_email(email, dns_resolver=dns_resolver)
52+
result = validate_email(email, dns_resolver=dns_resolver, **options)
3953
print(json.dumps(result.as_dict(), indent=2, sort_keys=True, ensure_ascii=False))
4054
except EmailNotValidError as e:
4155
print(e)

0 commit comments

Comments
 (0)