2020-09-11 09:25:19 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2024-01-01 02:43:48 -06:00
|
|
|
# Copyright (C) 2013 - 2024, The pgAdmin Development Team
|
2020-09-11 09:25:19 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
2024-09-19 03:33:43 -05:00
|
|
|
import email_validator
|
2021-07-05 02:25:40 -05:00
|
|
|
from email_validator import validate_email as email_validate, \
|
|
|
|
EmailNotValidError
|
2020-09-11 09:25:19 -05:00
|
|
|
|
|
|
|
|
2024-09-19 03:33:43 -05:00
|
|
|
def validate_email(email, email_config=None):
|
2021-07-05 02:25:40 -05:00
|
|
|
try:
|
2024-09-19 03:33:43 -05:00
|
|
|
if email_config is None:
|
|
|
|
email_config = {}
|
2024-07-18 00:35:37 -05:00
|
|
|
import config
|
2024-09-19 03:33:43 -05:00
|
|
|
email_config['CHECK_EMAIL_DELIVERABILITY'] = \
|
|
|
|
config.CHECK_EMAIL_DELIVERABILITY
|
|
|
|
email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'] = \
|
|
|
|
config.ALLOW_SPECIAL_EMAIL_DOMAINS
|
|
|
|
|
|
|
|
# Allow special email domains
|
|
|
|
if isinstance(email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'], str):
|
|
|
|
email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'] = \
|
|
|
|
email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'].split(',')
|
|
|
|
|
|
|
|
try:
|
|
|
|
email_validator.SPECIAL_USE_DOMAIN_NAMES = [
|
|
|
|
d for d in email_validator.SPECIAL_USE_DOMAIN_NAMES
|
|
|
|
if d not in email_config['ALLOW_SPECIAL_EMAIL_DOMAINS']
|
|
|
|
]
|
|
|
|
except Exception:
|
|
|
|
pass
|
2024-07-18 00:35:37 -05:00
|
|
|
|
2021-07-05 02:25:40 -05:00
|
|
|
# Validate.
|
2024-01-24 07:03:43 -06:00
|
|
|
_ = email_validate(
|
2024-09-19 03:33:43 -05:00
|
|
|
email,
|
|
|
|
check_deliverability=email_config['CHECK_EMAIL_DELIVERABILITY'])
|
2020-09-11 09:25:19 -05:00
|
|
|
|
2021-07-05 02:25:40 -05:00
|
|
|
# Update with the normalized form.
|
|
|
|
return True
|
|
|
|
except EmailNotValidError as e:
|
|
|
|
# email is not valid, exception message is human-readable
|
|
|
|
print(str(e))
|
2020-09-11 09:25:19 -05:00
|
|
|
return False
|