admin管理员组

文章数量:1401401

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.

Share Improve this question asked Mar 22 at 16:41 Super SanglapSuper Sanglap 914 bronze badges 1
  • 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
Add a comment  | 

1 Answer 1

Reset to default 2

The "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™.

本文标签: pythondynamically finding SMTP Server using Email AddressStack Overflow