I am trying to make a Python
function to find SMTP
host and port of an Email address
import dns.resolver
def get_smtp_host(email):
try:
# Extract the domain from the email address
domain = email.split('@')[1]
# Query the MX records for the domain
mx_records = dns.resolver.resolve(domain, 'MX')
# Sort the MX records by priority (lower number means higher priority)
sorted_mx_records = sorted(mx_records, key=lambda x: x.preference)
# Get the highest priority MX record
highest_priority_mx = sorted_mx_records[0].exchange.to_text()
# Return the SMTP host
return highest_priority_mx
except Exception as e:
return f"Error: {e}"
# Example usage
email = "[email protected]"
smtp_host = get_smtp_host(email)
print(smtp_host)
I am getting output of the MX record
. Cannot find a way to get SMTP host and port.
I am trying to make a Python
function to find SMTP
host and port of an Email address
import dns.resolver
def get_smtp_host(email):
try:
# Extract the domain from the email address
domain = email.split('@')[1]
# Query the MX records for the domain
mx_records = dns.resolver.resolve(domain, 'MX')
# Sort the MX records by priority (lower number means higher priority)
sorted_mx_records = sorted(mx_records, key=lambda x: x.preference)
# Get the highest priority MX record
highest_priority_mx = sorted_mx_records[0].exchange.to_text()
# Return the SMTP host
return highest_priority_mx
except Exception as e:
return f"Error: {e}"
# Example usage
email = "[email protected]"
smtp_host = get_smtp_host(email)
print(smtp_host)
I am getting output of the MX record
. Cannot find a way to get SMTP host and port.
- I don't understand what you are asking. The MX record is the name of the email server you should connect to. The ports are defined in the SMTP RFCs, e.g., 25 for plain SMTP. – Robert Commented Mar 23 at 16:42
1 Answer
Reset to default 2The "output of the MX record" literally is the SMTP host. With caveats:
There may be multiple MX records, in which case all of them are valid SMTP hosts and I don't understand why you're deliberately discarding them.
There also may be zero MX records, in which case the domain itself acts as the SMTP host. Your code should handle
except dns.resolver.NoAnswer
(i.e. domain exists but has no requested records) and return the email domain in such cases, not assume that the domain can't receive email. It's reasonable to ask the user to double-check such addresses, but you oughtn't reject them.There may also be a "null" MX record pointing to
"."
, which is supposed to mean "this domain has no MX whatsoever, not even the domain itself" and then you can assume that the domain can't receive mail.
But those aside, your current code returns gmail-smtp-in.l.google.
for the Gmail address, and that is indeed the highest-priority SMTP host for Gmail.
The MX port is always 25, without exceptions.
Other notes:
The answer object has a method
mx_records.rrset.processing_order()
to get a priority-sorted list, so you shouldn't need to do that manually. This should work not just for MX but also SRV and SVCB/HTTPS records.Use
.rsplit()
for the email address. Quoted local-parts may have an@
in them, e.g."Real @address"@example
, but the domain never will, so although you shouldn't encounter such weird addresses in practice, splitting the domain from the right is still more Correct™.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744308139a4567817.html
评论列表(0条)