🛠️ToolsShed

이메일 주소 검증기

단일 또는 여러 이메일 주소의 형식을 검증합니다.

자주 묻는 질문

코드 구현

import re
import dns.resolver  # pip install dnspython

def validate_email_format(email: str) -> bool:
    """Validate email format using RFC 5321/5322 rules."""
    pattern = r'^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$'
    if not re.match(pattern, email):
        return False
    local, domain = email.rsplit('@', 1)
    if len(email) > 254 or len(local) > 64:
        return False
    if '..' in email or email.startswith('.') or email.endswith('.'):
        return False
    return True

def check_mx_record(domain: str) -> bool:
    """Check if domain has MX records (can receive email)."""
    try:
        answers = dns.resolver.resolve(domain, 'MX')
        return len(answers) > 0
    except Exception:
        return False

def validate_email(email: str, check_mx: bool = False) -> dict:
    result = {
        "email": email,
        "format_valid": validate_email_format(email),
        "mx_valid": None,
    }
    if result["format_valid"] and check_mx:
        domain = email.split('@')[1]
        result["mx_valid"] = check_mx_record(domain)
    return result

# Usage
emails = ["user@example.com", "invalid@", "test..user@domain.com"]
for email in emails:
    print(validate_email(email))

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.