|
10 | 10 | # invalid email addresses. When passing an email address on the command |
11 | 11 | # line, if the email address is valid, information about it will be printed. |
12 | 12 | # 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). |
13 | 16 |
|
14 | 17 | import json |
| 18 | +import os |
15 | 19 | import sys |
16 | 20 |
|
17 | 21 | from .validate_email import validate_email |
|
22 | 26 | def main(dns_resolver=None): |
23 | 27 | # The dns_resolver argument is for tests. |
24 | 28 |
|
| 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 | + |
25 | 39 | if len(sys.argv) == 1: |
26 | 40 | # Validate the email addresses pased line-by-line on STDIN. |
27 | 41 | dns_resolver = dns_resolver or caching_resolver() |
28 | 42 | for line in sys.stdin: |
29 | 43 | email = line.strip() |
30 | 44 | try: |
31 | | - validate_email(email, dns_resolver=dns_resolver) |
| 45 | + validate_email(email, dns_resolver=dns_resolver, **options) |
32 | 46 | except EmailNotValidError as e: |
33 | 47 | print(f"{email} {e}") |
34 | 48 | else: |
35 | 49 | # Validate the email address passed on the command line. |
36 | 50 | email = sys.argv[1] |
37 | 51 | try: |
38 | | - result = validate_email(email, dns_resolver=dns_resolver) |
| 52 | + result = validate_email(email, dns_resolver=dns_resolver, **options) |
39 | 53 | print(json.dumps(result.as_dict(), indent=2, sort_keys=True, ensure_ascii=False)) |
40 | 54 | except EmailNotValidError as e: |
41 | 55 | print(e) |
|
0 commit comments