@@ -28,7 +28,7 @@ def __init__(self):
28
28
self .domain = os .getenv ("DOMAIN" , "" ).strip ()
29
29
30
30
# 如果临时邮箱为null则加载IMAP
31
- if self .temp_mail == ' null' :
31
+ if self .temp_mail == " null" :
32
32
self .imap = True
33
33
self .imap_server = os .getenv ("IMAP_SERVER" , "" ).strip ()
34
34
self .imap_port = os .getenv ("IMAP_PORT" , "" ).strip ()
@@ -50,40 +50,76 @@ def get_imap(self):
50
50
"imap_port" : self .imap_port ,
51
51
"imap_user" : self .imap_user ,
52
52
"imap_pass" : self .imap_pass ,
53
- "imap_dir" : self .imap_dir
53
+ "imap_dir" : self .imap_dir ,
54
54
}
55
55
56
56
def get_domain (self ):
57
57
return self .domain
58
58
59
59
def check_config (self ):
60
- if not self .check_is_valid (self .temp_mail ):
61
- raise ValueError ("临时邮箱未配置,请在 .env 文件中设置 TEMP_MAIL" )
62
- if not self .check_is_valid (self .domain ):
63
- raise ValueError ("域名未配置,请在 .env 文件中设置 DOMAIN" )
64
- if not self .imap_server == 'null' and not self .check_is_valid (self .imap_server ):
65
- raise ValueError ("IMAP服务器未配置,请在 .env 文件中设置 IMAP_SERVER" )
66
- if not self .imap_port == 'null' and not self .check_is_valid (self .imap_port ):
67
- raise ValueError ("IMAP端口未配置,请在 .env 文件中设置 IMAP_PORT" )
68
- if not self .imap_user == 'null' and not self .check_is_valid (self .imap_user ):
69
- raise ValueError ("IMAP用户名未配置,请在 .env 文件中设置 IMAP_USER" )
70
- if not self .imap_pass == 'null' and not self .check_is_valid (self .imap_pass ):
71
- raise ValueError ("IMAP密码未配置,请在 .env 文件中设置 IMAP_PASS" )
72
- if not self .imap_dir == 'null' and not self .check_is_valid (self .imap_dir ):
73
- raise ValueError ("IMAP收件箱目录未配置,请在 .env 文件中设置 IMAP_DIRECTORY" )
74
-
75
- def check_is_valid (self , str ):
76
- return len (str .strip ()) > 0
60
+ """检查配置项是否有效
61
+
62
+ 检查规则:
63
+ 1. 如果使用 tempmail.plus,需要配置 TEMP_MAIL 和 DOMAIN
64
+ 2. 如果使用 IMAP,需要配置 IMAP_SERVER、IMAP_PORT、IMAP_USER、IMAP_PASS
65
+ 3. IMAP_DIR 是可选的
66
+ """
67
+ # 基础配置检查
68
+ required_configs = {
69
+ "domain" : "域名" ,
70
+ }
71
+
72
+ # 检查基础配置
73
+ for key , name in required_configs .items ():
74
+ if not self .check_is_valid (getattr (self , key )):
75
+ raise ValueError (f"{ name } 未配置,请在 .env 文件中设置 { key .upper ()} " )
76
+
77
+ # 检查邮箱配置
78
+ if self .temp_mail != "null" :
79
+ # tempmail.plus 模式
80
+ if not self .check_is_valid (self .temp_mail ):
81
+ raise ValueError ("临时邮箱未配置,请在 .env 文件中设置 TEMP_MAIL" )
82
+ else :
83
+ # IMAP 模式
84
+ imap_configs = {
85
+ "imap_server" : "IMAP服务器" ,
86
+ "imap_port" : "IMAP端口" ,
87
+ "imap_user" : "IMAP用户名" ,
88
+ "imap_pass" : "IMAP密码" ,
89
+ }
90
+
91
+ for key , name in imap_configs .items ():
92
+ value = getattr (self , key )
93
+ if value == "null" or not self .check_is_valid (value ):
94
+ raise ValueError (
95
+ f"{ name } 未配置,请在 .env 文件中设置 { key .upper ()} "
96
+ )
97
+
98
+ # IMAP_DIR 是可选的,如果设置了就检查其有效性
99
+ if self .imap_dir != "null" and not self .check_is_valid (self .imap_dir ):
100
+ raise ValueError (
101
+ "IMAP收件箱目录配置无效,请在 .env 文件中正确设置 IMAP_DIR"
102
+ )
103
+
104
+ def check_is_valid (self , value ):
105
+ """检查配置项是否有效
106
+
107
+ Args:
108
+ value: 配置项的值
109
+
110
+ Returns:
111
+ bool: 配置项是否有效
112
+ """
113
+ return isinstance (value , str ) and len (str (value ).strip ()) > 0
77
114
78
115
def print_config (self ):
79
- # logging.info(f"\033[32m临时邮箱: {self.temp_mail}\033[0m")
80
116
if self .imap :
81
117
logging .info (f"\033 [32mIMAP服务器: { self .imap_server } \033 [0m" )
82
118
logging .info (f"\033 [32mIMAP端口: { self .imap_port } \033 [0m" )
83
119
logging .info (f"\033 [32mIMAP用户名: { self .imap_user } \033 [0m" )
84
120
logging .info (f"\033 [32mIMAP密码: { '*' * len (self .imap_pass )} \033 [0m" )
85
121
logging .info (f"\033 [32mIMAP收件箱目录: { self .imap_dir } \033 [0m" )
86
- if self .temp_mail != ' null' :
122
+ if self .temp_mail != " null" :
87
123
logging .info (f"\033 [32m临时邮箱: { self .temp_mail } @{ self .domain } \033 [0m" )
88
124
logging .info (f"\033 [32m域名: { self .domain } \033 [0m" )
89
125
@@ -93,7 +129,6 @@ def print_config(self):
93
129
try :
94
130
config = Config ()
95
131
print ("环境变量加载成功!" )
96
- config .get_temp_mail ()
97
- config .get_domain ()
132
+ config .print_config ()
98
133
except ValueError as e :
99
134
print (f"错误: { e } " )
0 commit comments