File size: 2,124 Bytes
7cf86e5
 
35429ce
7cf86e5
 
 
 
 
 
b0b394a
7cf86e5
 
 
 
b0b394a
7cf86e5
 
 
 
 
 
35429ce
7cf86e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0b394a
7cf86e5
b0b394a
7cf86e5
b0b394a
 
 
7cf86e5
 
 
 
 
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
import requests
import gradio as gr
from utils import API_SMTP, EN_US

ZH2EN = {
    "收信人邮箱": "To email",
    "标题": "Title",
    "测试标题": "Test title",
    "正文": "Content",
    "SMTP 在线测试工具": "SMTP online tester",
    "发信人昵称": "Sender name",
    "测试昵称": "Test nickname",
    "发信人邮箱": "From email",
    "应用密钥": "API key",
    "SMTP 服务器": "SMTP host",
    "端口": "Port",
    "发送状态": "Status",
}


def _L(zh_txt: str):
    return ZH2EN[zh_txt] if EN_US else zh_txt


def infer(target, title, content, name, email, password, host, port):
    try:
        response = requests.get(
            API_SMTP,
            params={
                "host": host,
                "Port": port,
                "key": password,  # apikey
                "email": email,  # from
                "mail": target,  # to
                "title": title,  # subject
                "name": name,  # nickname
                "text": content,  # content
            },
        )
        if response.status_code == 200:
            result: dict = response.json()
            return result.get("status")

        else:
            raise ConnectionError(f"{response.status_code}")

    except Exception as e:
        return f"{e}"


def smtp_tester():
    return gr.Interface(
        fn=infer,
        inputs=[
            gr.Textbox(label=_L("收信人邮箱"), placeholder="Recipient"),
            gr.Textbox(label=_L("标题"), value=_L("测试标题")),
            gr.TextArea(label=_L("正文"), value=_L("SMTP 在线测试工具")),
            gr.Textbox(label=_L("发信人昵称"), value=_L("测试昵称")),
            gr.Textbox(label=_L("发信人邮箱"), placeholder="Sender"),
            gr.Textbox(label=_L("应用密钥"), placeholder="SMTP password"),
            gr.Textbox(label=_L("SMTP 服务器"), value="smtp.163.com"),
            gr.Slider(label=_L("端口"), minimum=0, maximum=65535, step=1, value=25),
        ],
        outputs=gr.TextArea(label=_L("发送状态"), show_copy_button=True),
        flagging_mode="never",
    )