34
34
except ImportError :
35
35
import json as simplejson
36
36
37
- import ConfigParser , logging
37
+ import ConfigParser , logging , argparse
38
38
import codecs , sys
39
39
40
40
from string import Template
@@ -210,11 +210,16 @@ def send(self, msg):
210
210
# return False
211
211
212
212
213
- def init_smtp ():
213
+ def init_smtp (ini_file ):
214
214
"""Load basic and smtp config from mail.ini
215
215
"""
216
216
config = ConfigParser .RawConfigParser (allow_no_value = True )
217
- config .read (os .path .join (sys .path [0 ],'mail.ini' ))
217
+ print ('read ini file' , ini_file )
218
+ if not os .path .exists (ini_file ):
219
+ # use ini in python dir
220
+ ini_file = os .path .join (os .path .abspath (os .path .dirname (sys .argv [0 ])),ini_file )
221
+ print ('try to use another place' , ini_file )
222
+ config .read (ini_file )
218
223
base_dir = config .get ('basic' , 'base_dir' )
219
224
if sys .platform == 'win32' : #decode to unicode
220
225
base_dir = base_dir .decode ('utf-8' )
@@ -249,20 +254,22 @@ def load_msg(section, config):
249
254
return MailTool .msg (ffrom , rcpt_tos , reply_to , subject , content , files )
250
255
251
256
252
- def main (args ):
253
- """
254
- python mail_tool.py test
255
- """
256
- section = 'test'
257
- if len (args ) > 1 :
258
- section = args [1 ]
257
+ def main ():
258
+ parser = argparse .ArgumentParser (prog = 'Mail Tool' , usage = '%(prog)s [options]' )
259
+
260
+ parser .add_argument ('-i' , dest = 'ini' , type = str , default = 'mail.ini' ,
261
+ help = 'Mail Config file' )
262
+ parser .add_argument ('-s' , dest = 'section' , type = str , default = 'test' ,
263
+ help = 'section in .ini file' )
264
+ args = parser .parse_args ()
265
+
259
266
260
267
# init smtp tool
261
- tool , base_dir , config = init_smtp ()
268
+ tool , base_dir , config = init_smtp (args . ini )
262
269
263
270
# send one mail
264
- print ('section = ' + section )
265
- msg = load_msg (section , config )
271
+ print ('section = ' + args . section )
272
+ msg = load_msg (args . section , config )
266
273
MailTool .show_msg (msg )
267
274
268
275
status = False
@@ -284,4 +291,7 @@ def main(args):
284
291
tool .close ()
285
292
286
293
if __name__ == '__main__' :
287
- main (sys .argv )
294
+ """
295
+ python mail_tool.py -i /tmp/mail.ini -s test
296
+ """
297
+ main ()
0 commit comments