2 #- * - encoding: utf-8 - * -
10 from email
import encoders
11 from email
.mime
.audio
import MIMEAudio
12 from email
.mime
.base
import MIMEBase
13 from email
.mime
.image
import MIMEImage
14 from email
.mime
.multipart
import MIMEMultipart
15 from email
.mime
.text
import MIMEText
17 from optparse
import OptionParser
21 def ReadConfig(filename
):
22 """ read param from config file: filename """
23 rc
=ConfigParser
.ConfigParser()
25 maxlen
=rc
.getint('global','maillen')*1024
26 smtpcount
=rc
.getint('global','smtp')
27 defaultsmtp
=rc
.get('global','default')
28 #only use default or use each smtp orderly
29 onlydefault
=rc
.getint('global','onlydefault')
30 tolist
=rc
.get('global','to').split(',')
33 param
.append([maxlen
,smtpcount
,defaultsmtp
,onlydefault
,tolist
])
35 for i
in xrange(1,smtpcount
+1):
37 key
=rc
.get(sname
,'name')
38 addr
=rc
.get(sname
,'addr')
39 port
=rc
.getint(sname
,'port')
40 ssl
=rc
.getint(sname
,'ssl')
41 username
=rc
.get(sname
,'user')
42 password
=rc
.get(sname
,'pass')
43 mailaddress
=rc
.get(sname
,'mail')
44 smpts
[key
]=[addr
,port
,ssl
,username
,password
,mailaddress
]
45 servernamelist
.append(key
)
47 param
.append(servernamelist
)
52 """ get option from command line """
53 parser
= OptionParser(usage
="""\
54 Send the contents of a directory as a MIME message.
56 Usage: %prog [options] [directory]
59 parser
.add_option('-s', '--subject',
60 type='string', action
='store', metavar
='SUBJECT',
61 help='The value of the Subject: header (required)')
62 parser
.add_option('-r', '--recipient',
63 type='string', action
='append', metavar
='RECIPIENT',
64 default
=[], dest
='recipients',
65 help='A To: header value (at least one required)')
66 parser
.add_option('', '--bcc',
67 type='string', action
='append', metavar
='BCC',
68 default
=[], dest
='bccs',
69 help='A BCC: send by BCC')
70 parser
.add_option('-f', '--file',
71 type='string', action
='append', metavar
='FILE',
72 default
=[], dest
='files',
73 help="A File point out the files to be mailed. Example: '*.jpg' , '../*.pdf' etc. DON NOT TO FORGET THE '. If this option , we will discard directory argument")
74 parser
.add_option('-c','--configfile',
75 type='string',action
='store',metavar
='CONFIGFILE',
76 help="The configfile, for Linux is '~/.xmailrc'")
77 parser
.add_option('-l','--listserver',
78 action
='store_true',dest
='listserver',
79 help="Only list the server in configfile")
80 parser
.add_option('', '--maxmaillen',
81 type='float', action
='store', metavar
='MAXMAILLEN',
82 help='The value of max length of each mail (M)')
84 opts
, args
= parser
.parse_args()
88 def ReadAlias(filename
):
89 """ read alias from filename """
91 afile
=open(filename
,'r')
92 s
=afile
.readline().strip()
94 name
,email
=s
.split(':')
95 aliasdict
[name
]=email
.split(',')
96 s
=afile
.readline().strip()
99 def MailAddressFromAlias(alist
,adict
):
100 """ realias the address alist from adict,
101 alist: list with alias
102 adict: dictionary of alias """
106 elist
.extend(adict
[i
])
111 def NextServ(cur
,lists
):
112 """ get the Next of cur in lists """
114 for i
in range(0,keylen
):
123 opts
,args
=GetOption()
131 rcfile
=opts
.configfile
133 if sys
.platform
=='win32':
134 rcfile
=os
.getcwd()+'\\xmail.ini'
135 aliasfile
=os
.getcwd()+'\\xmail.alias'
138 rcfile
=os
.environ
['HOME']+'/.xmailrc'
139 aliasfile
=os
.environ
['HOME']+'/.xmailalias'
142 param
=ReadConfig(rcfile
)
149 maxmaillen
=glparam
[0] # max mail length
150 maxsmtpser
=glparam
[1] # max smtp server
151 defaultsmtp
=glparam
[2] # default smtp server
152 onlydefault
=glparam
[3] # only use default smtp server?
153 tolist
=glparam
[4] # to list
155 mailalias
=ReadAlias(aliasfile
)
158 maxmaillen
=int(opts
.maxmaillen
*1024*1024)
159 print 'Length for each mail is less then ',maxmaillen
161 sender
=smtpser
.SMTPServer()
163 sender
.SetInfoFromList(serdict
[i
])
166 if opts
.recipients
: # 如果命令行指定了dest e-mail address
167 tolist
=opts
.recipients
# send to lists
169 print 'use -r to add recipients'
171 tolist
=MailAddressFromAlias(tolist
,mailalias
)
172 if opts
.bccs
: # add bcc
174 bcclist
=MailAddressFromAlias(bcclist
,mailalias
)
179 subj
=str(email
.Header
.Header(opts
.subject
,charset
))
181 subj
='X-Mailer: send directory'
183 sender
=smtpser
.SMTPServer()
184 msender
=smtpser
.MailSenders(serdict
,defaultsmtp
)
185 msender
.OnlyDefault(onlydefault
)
186 msender
.SetSubject(subj
,charset
)
187 msender
.SetCharset(charset
)
190 print 'X-Mailer , mail total directory to dest e-mail address'
191 #print 'Directory:'+direction
192 print 'To:'+' '.join(tolist
)
194 print 'BCC:'+' '.join(bcclist
)
195 print 'use %d SMTP server(s):'%len(serdict
),serdict
.keys()
200 cursmtp
=defaultsmtp
# current used smtp server
205 for fl
in opts
.files
:
207 fileslist
=fileslist
+a
209 fileslist
=glob
.glob(direction
+'/*') # files in directory
210 fileslist
.sort() # sort it
211 print 'We will mail %d file(s):'%len(fileslist
)+' '.join(fileslist
)
213 # the sender smtp server
214 sender
.SetInfoFromList(serdict
[cursmtp
])
216 for filename
in fileslist
:
217 path
=filename
#os.path.join(direction,filename)
218 if not os
.path
.isfile(path
):
220 filelen
=os
.stat(path
).st_size
221 if filelen
> maxmaillen
: # one big file
222 print 'Big file:'+filename
223 # print 'From:'+sender.email
224 # print 'Subject:'+subj
225 bodymsg
=msender
.CreateBodyMail([filename
,'<a href=mailto:r01ustc+xmailer@gmail.com>Contact me</a>'],charset
)
226 msender
.MailBigFile(tolist
,subj
,bodymsg
,path
,maxmaillen
,charset
=charset
,bcc
=bcclist
)
227 # sender.MailBigFile(tolist,subj,[filename,'<a href=mailto:r01ustc+xmailer@gmail.com>联系我</a>'],path,maxmaillen,str(email.Header.Header(os.path.basename(filename),charset)))
228 # if not onlydefault:
229 # cursmtp=NextServ(cursmtp,serdict.keys())
230 # sender.SetInfoFromList(serdict[cursmtp])
232 if fileslen
+filelen
> maxmaillen
: # 加上新文件后就超大了
233 print 'Mail Files:'+' '.join(files
)
234 # print 'From:'+sender.email
235 # print 'Subject:'+subj
236 bodymsg
=msender
.CreateBodyMail(['\n'.join(files
),'<a href=mailto:r01ustc+xmailer@gmail.com>Contact me</a>'],charset
)
237 msender
.MailFiles(tolist
,subj
,bodymsg
,files
,charset
=charset
,bcc
=bcclist
)
238 # sender.MailFiles(tolist,subj,['\n'.join(files),'<a href=mailto:r01ustc+xmailer@gmail.com>联系我</a>'],files,charset)
239 # if not onlydefault:
240 # cursmtp=NextServ(cursmtp,serdict.keys())
241 # sender.SetInfoFromList(serdict[cursmtp])
244 else: # add new file name to list
246 fileslen
=fileslen
+filelen
247 if files
: # has some file not mailed
248 print 'Mail Files remained :'+' '.join(files
)
249 # print 'From:'+sender.email
250 # print 'Subject:'+subj
251 bodymsg
=msender
.CreateBodyMail(['\n'.join(files
),'<a href=mailto:r01ustc+xmailer@gmail.com>Contact me</a>'],charset
)
252 msender
.MailFiles(tolist
,subj
,bodymsg
,files
,charset
=charset
,bcc
=bcclist
)
253 # sender.MailFiles(tolist,subj,['\n'.join(files),'<a href=mailto:r01ustc+xmailer@gmail.com>联系我</a>'],files,charset)