(email, email_config=None)
| 13 | |
| 14 | |
| 15 | def validate_email(email, email_config=None): |
| 16 | # email_validator raises TypeError (not EmailNotValidError) when the |
| 17 | # input is not str/bytes. Treat anything non-string as invalid so |
| 18 | # callers see a clean False, matching the contract that this wrapper |
| 19 | # never raises. |
| 20 | if not isinstance(email, (str, bytes)): |
| 21 | return False |
| 22 | try: |
| 23 | if email_config is None: |
| 24 | email_config = {} |
| 25 | import config |
| 26 | email_config['CHECK_EMAIL_DELIVERABILITY'] = \ |
| 27 | config.CHECK_EMAIL_DELIVERABILITY |
| 28 | email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'] = \ |
| 29 | config.ALLOW_SPECIAL_EMAIL_DOMAINS |
| 30 | email_config["GLOBALLY_DELIVERABLE"] = \ |
| 31 | config.GLOBALLY_DELIVERABLE |
| 32 | |
| 33 | # Allow special email domains |
| 34 | if isinstance(email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'], str): |
| 35 | email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'] = \ |
| 36 | email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'].split(',') |
| 37 | |
| 38 | try: |
| 39 | email_validator.SPECIAL_USE_DOMAIN_NAMES = [ |
| 40 | d for d in email_validator.SPECIAL_USE_DOMAIN_NAMES |
| 41 | if d not in email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'] |
| 42 | ] |
| 43 | except Exception: |
| 44 | pass |
| 45 | |
| 46 | email_validator.GLOBALLY_DELIVERABLE = \ |
| 47 | email_config["GLOBALLY_DELIVERABLE"] |
| 48 | |
| 49 | # Validate. |
| 50 | _ = email_validate( |
| 51 | email, |
| 52 | check_deliverability=email_config['CHECK_EMAIL_DELIVERABILITY']) |
| 53 | |
| 54 | # Update with the normalized form. |
| 55 | return True |
| 56 | except EmailNotValidError as e: |
| 57 | # email is not valid, exception message is human-readable |
| 58 | print(str(e)) |
| 59 | return False |
no outgoing calls