gpt4 book ai didi

python - 验证电子邮件地址是否确实存在的最佳方法是什么?

转载 作者:行者123 更新时间:2023-12-02 16:05:52 25 4
gpt4 key购买 nike

是否有任何方法可以验证电子邮件是否在 python 中实际存在?或者你们有提供此类服务的平台吗?例如:

我有一些电子邮件

email1@google.com

email2@gmail.com

asdasd@adasd.com

asdasdasdasdasd@gmail.com

我如何才能 100% 确定互联网上确实存在以下电子邮件中的哪一个?

最佳答案

网上有很多教程。这是我发现的...


First you need to check for the correct formatting and for this you can use regular expressions like this:

   import re

addressToVerify ='info@scottbrady91.com'
match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', addressToVerify)

if match == None:
print('Bad Syntax')
raise ValueError('Bad Syntax')

DNS

Next we need to get the MX record for the target domain, in order to start the email verification process. Note that you are allowed in the RFCs to have a mail server on your A record, but that's outside of the scope of this article and demo script.

    import dns.resolver

records = dns.resolver.query('scottbrady91.com', 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)

Python DNS Python doesn't have any inbuilt DNS components, so we'vepulled in the popular dnspython library. Any library that can resolvean MX record from a domain name will work though.

Mailbox

Now that we have all the preflight information we need, we can now find out if the email address exists.

   import socket
import smtplib

# Get local server hostname
host = socket.gethostname()

# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP()
server.set_debuglevel(0)

# SMTP Conversation
server.connect(mxRecord)
server.helo(host)
server.mail('me@domain.com')
code, message = server.rcpt(str(addressToVerify))
server.quit()

# Assume 250 as Success
if code == 250:
print('Success')
else:
print('Bad')

What we are doing here is the first three commands of an SMTP conversation for sending an email, stopping just before we send any data.

The actual SMTP commands issued are: HELO, MAIL FROM and RCPT TO. It is the response to RCPT TO that we are interested in. If the server sends back a 250, then that means we are good to send an email (the email address exists), otherwise the server will return a different status code (usually a 550), meaning the email address does not exist on that server.

And that's email verification!

来源:https://www.scottbrady91.com/Email-Verification/Python-Email-Verification-Script


这里有一些来自另一个网站的其他选择...

Let’s make it more sophisticated and assume we want the following criteria to be met for an account@domain email address:

  • Both consist of case-insensitive alphanumeric characters. Dashes,periods, hyphens or underscores are also allowed
  • Both can only start and end with alphanumeric characters
  • Both contain no white spaces account has at least one character anddomain has at least two domain includes at least one period
  • And of course, there’s an ‘@’ symbol between them
    ^[a-z]([w-]*[a-z]|[w-.]*[a-z]{2,}|[a-z])*@[a-z]([w-]*[a-z]|[w-.]*[a-z]{2,}|[a-z]){4,}?.[a-z]{2,}$

Validating emails with Python libraries

Another way of running sophisticated checks is with ready-to-use packages, and there are plenty to choose from. Here are several popular options:

email-validator 1.0.5

This library focuses strictly on the domain part of an email address, and checks if it’s in an x@y.com format.

As a bonus, the library also comes with a validation tool. It checks if the domain name can be resolved, thus giving you a good idea about its validity.

pylsEmail 1.3.2

This Python program to validate email addresses can also be used to validate both a domain and an email address with just one call. If a given address can’t be validated, the script will inform you of the most likely reasons.

py3-validate-email

This comprehensive library checks an email address for a proper structure, but it doesn’t just stop there.

It also verifies if the domain’s MX records exist (as in, is able to send/receive emails), whether a particular address on this domain exists, and also if it’s not blacklisted. This way, you avoid emailing blacklisted accounts, which would affect deliverability.

来源:https://mailtrap.io/blog/python-validate-email/

这些只是这些网站的摘录,请务必访问它们并确认这对您的具体情况有用,它们还包含更多方法并更详细地解释这一点,希望对您有所帮助。

关于python - 验证电子邮件地址是否确实存在的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69412522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com