First simple method Regex
The re
module is very useful for this purpose. Here’s a common regular expression pattern that captures most valid email addresses:
- The email starts with a combination of letters, digits, dots, hyphens, or underscores.
- Followed by the
@
symbol. - Followed by the domain name, which is a combination of letters, digits, dots, and hyphens.
- The domain ends with a top-level domain (TLD) which is at least two characters long.
contacts = Contact.objects.filter(is_checked=True, email__contains='info@', for_mailing=True)
# Select all contacten dat groen staan voor mailing
for contact in contacts:
if self.validate_email(contact.email):
print("Valid")
else:
print("Not valid")
contact.is_checked = False
contact.for_mailing = False
contact.save()
def validate_email(self, email):
# Regular expression for validating an Email
email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
# Match the input email against the regular expression
if re.match(email_regex, email):
return True
else:
return False
Email-validator python lib via DNS
emailinfo = validate_email(email, check_deliverability=False)
not worked in my setup..
Self-written script
You can then create a command that visits the specified website to check if the domain is still active and to locate an email address on the contact page. If you need to verify company emails, validate personal emails, or perform real-time email validation within your application, you could use an API like SendGrid's.
Email-address-validation via API
from sendgrid import SendGridAPIClient
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
data = {
"source": "Newsletter",
"email": "example@example.com"
}
response = sg.client.validations.email.post(
request_body=data
)
print(response.status_code)
print(response.body)
print(response.headers)
dnspython
library if you haven't already:<ipython-input-5-55aafb908ebf> in <module>()
----> 1 a_records = dns.resolver.resolve(domain, 'A')
AttributeError: 'module' object has no attribute 'resolve'
print(domain)
try:
# Check for A record
a_records = dns.resolver.query(domain, 'A')
if not a_records:
msg = "No A records found for domain: " + domain
print(msg)
contact.comment = msg
return False
else:
print("A records for domain " + domain)
for record in a_records:
print(record)
# Check for MX record
mx_records = dns.resolver.query(domain, 'MX')
if not mx_records:
msg = "No MX records found for domain: " + domain
print(msg)
contact.comment = msg
return False
else:
print("MX records for domain:")
for record in mx_records:
print(record.exchange, record.preference)
return True
except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN, dns.resolver.NoNameservers, dns.exception.Timeout, dns.name.EmptyLabel) as e:
msg = "Error checking DNS records for domain " + domain
print(msg)
contact.comment = msg
return False
Bonus funciton to generate name from email address
def split_email(self, email):
# Extract the part before '@'
local_part = email.split('@')[0]
# Extract the domain part after '@'
domain_part = email.split('@')[1]
# Remove the domain extension
domain_without_extension = '.'.join(domain_part.split('.')[:-1])
# Replace hyphens with spaces and capitalize each word
formatted_domain = ' '.join(word.capitalize() for word in re.split('-|_', domain_without_extension))
return formatted_domain
Comments
Post a Comment