File size: 2,681 Bytes
2ec2072
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
import re
import dns.resolver
import socket
import smtplib
from typing import Tuple, Union

def check_syntax(mail_address: str) -> bool:
    """์ด๋ฉ”์ผ ์ฃผ์†Œ ๊ตฌ๋ฌธ ๊ฒ€์‚ฌ"""
    match = re.match('[A-Za-z0-9._+]+@[A-Za-z]+.[A-Za-z]', mail_address)
    return match is not None

def check_dns(domain: str) -> Tuple[bool, str]:
    """DNS MX ๋ ˆ์ฝ”๋“œ ๊ฒ€์‚ฌ"""
    try:
        records = dns.resolver.query(domain, 'MX')
        mx_record = str(records[0].exchange)
        return True, mx_record
    except Exception as e:
        return False, str(e)

def check_smtp(mail_address: str, mx_record: str) -> bool:
    """SMTP ์„œ๋ฒ„ ์—ฐ๊ฒฐ ๊ฒ€์‚ฌ"""
    try:
        local_host = socket.gethostname()
        server = smtplib.SMTP(timeout=10)
        server.set_debuglevel(0)
        server.connect(mx_record)
        server.helo(local_host)
        server.mail('[email protected]')
        code, message = server.rcpt(str(mail_address))
        server.quit()
        return code == 250
    except Exception:
        return False

def validate_email(mail_address: str) -> str:
    """์ด๋ฉ”์ผ ์ฃผ์†Œ ์ข…ํ•ฉ ๊ฒ€์ฆ"""
    # ๊ฒฐ๊ณผ ๋ฉ”์‹œ์ง€ ์ดˆ๊ธฐํ™”
    result_messages = []
    
    # 1. ๊ตฌ๋ฌธ ๊ฒ€์‚ฌ
    if not check_syntax(mail_address):
        return "โŒ ์ด๋ฉ”์ผ ์ฃผ์†Œ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค."
    result_messages.append("โœ… ์ด๋ฉ”์ผ ์ฃผ์†Œ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฆ…๋‹ˆ๋‹ค.")
    
    # 2. ๋„๋ฉ”์ธ ์ถ”์ถœ
    domain = mail_address.split('@')[1]
    
    # 3. DNS MX ๋ ˆ์ฝ”๋“œ ๊ฒ€์‚ฌ
    dns_valid, mx_record = check_dns(domain)
    if not dns_valid:
        return "\n".join(result_messages + ["โŒ ๋„๋ฉ”์ธ์˜ ๋ฉ”์ผ ์„œ๋ฒ„๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."])
    result_messages.append("โœ… ๋„๋ฉ”์ธ์˜ ๋ฉ”์ผ ์„œ๋ฒ„๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.")
    
    # 4. SMTP ๊ฒ€์‚ฌ
    if not check_smtp(mail_address, mx_record):
        return "\n".join(result_messages + ["โŒ ์ด๋ฉ”์ผ ์ฃผ์†Œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ํ™•์ธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."])
    result_messages.append("โœ… ์ด๋ฉ”์ผ ์ฃผ์†Œ๊ฐ€ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.")
    
    return "\n".join(result_messages)

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
iface = gr.Interface(
    fn=validate_email,
    inputs=gr.Textbox(label="์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”", placeholder="[email protected]"),
    outputs=gr.Textbox(label="๊ฒ€์ฆ ๊ฒฐ๊ณผ"),
    title="์ด๋ฉ”์ผ ์ฃผ์†Œ ๊ฒ€์ฆ ๋„๊ตฌ",
    description="์ž…๋ ฅ๋œ ์ด๋ฉ”์ผ ์ฃผ์†Œ์˜ ์œ ํšจ์„ฑ์„ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„์— ๊ฑธ์ณ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.",
    examples=[
        ["[email protected]"],
        ["[email protected]"],
        ["malformed@@email.com"]
    ]
)

# ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
if __name__ == "__main__":
    iface.launch()