Initial commit
bn8 authored 11 years ago
|
9) #
10) # This file is part of MailT.
11) #
12) # MailT is free software; you can redistribute it and/or modify
13) # it under the terms of the GNU General Public License as published
14) # by the Free Software Foundation, either version 3 of the License,
15) # or (at your option) any later version.
16) #
17) # MailT is distributed in the hope that it will be useful,
18) # but WITHOUT ANY WARRANTY; without even the implied warranty of
19) # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20) # GNU Affero General Public License for more details.
21) #
22) # You should have received a copy of the GNU General Public License
23) # along with this program. If not, see <http://www.gnu.org/licenses/>.
24)
25) from optparse import OptionParser
26)
27) import os
28) import imaplib
29)
30) import logging
31) import sys
32)
33) parser = OptionParser()
34)
35)
36) parser.add_option('-H',
37) '--host',
38) action="store",
39) type="string",
40) dest="host",
41) help="The IMAP host",
42) default="127.0.0.1")
43)
44) parser.add_option('-p',
45) '--port',
46) action="store",
47) type="string",
48) dest="port",
49) help="The IMAP port",
50) default=None)
51)
52) parser.add_option('-S',
53) '--ssl',
54) action="store_true",
55) dest="ssl",
56) help="IMAP Over SSL (IMAPS)",
57) default=False)
58)
59) parser.add_option('-U',
60) '--user',
61) action="store",
62) type="string",
63) dest="user",
64) help="User login",
65) default=None)
66)
67) parser.add_option('-P',
68) '--password',
69) action="store",
70) type="string",
71) dest="password",
72) help="User password",
73) default=None)
74)
75) parser.add_option('--md5',
76) action="store_true",
77) dest="md5",
78) help="Use CRAM-MD5 password for authentication",
79) default=False)
80)
81) parser.add_option('-m',
82) '--mailbox',
83) action="store",
84) type="string",
85) dest="mailbox",
86) help="The mailbox directory to select",
87) default='INBOX')
88)
89) parser.add_option('-v',
90) '--verbose',
91) action="store_true",
92) dest="verbose")
93)
|
Initial commit
bn8 authored 11 years ago
|
108) (options, args) = parser.parse_args()
109)
110) if options.verbose:
111) loglevel=logging.DEBUG
112) else:
113) loglevel=logging.INFO
114)
115) logging.basicConfig(level=loglevel,format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
116)
117) if not options.user or not options.password:
118) logging.fatal('You must provide the user login and password')
119) sys.exit(1)
120)
121) if options.port is None:
122) if options.ssl:
123) options.port=993
124) else:
125) options.port=143
126)
127) if options.ssl:
128) logging.debug('Establish SSL connexion to server')
129) try:
130) server = imaplib.IMAP4_SSL(options.host,options.port)
131) logging.debug('Connected')
132) except Exception, e:
133) logging.critical("IMAP connection error : %s" % e)
134) sys.exit(2)
135) else:
136) logging.debug("Establish connexion to server")
137) try:
138) server = imaplib.IMAP4(options.host,options.port)
139) logging.debug('Connected')
140) except Exception, e:
141) logging.critical("IMAP connection error : %s" % e)
142) sys.exit(2)
143)
144) if options.user:
145) try:
146) if options.md5:
147) logging.debug('Login into server as %s (with CRAM-MD5 password)' % options.user)
148) server.login_cram_md5(options.user,options.password)
149) else:
150) logging.debug('Login into server as %s' % options.user)
151) server.login(options.user,options.password)
152) logging.debug('Logged')
153) except Exception as e:
154) logging.critical("IMAP Auth error : %s" % e)
155) sys.exit(3)
|
imapt : Add --expunge param...
Benjamin Renard authored 11 years ago
|
156)
157) def expunge(server,mailbox):
158) logging.info('Expunge %s mailbox' % mailbox)
159) try:
160) (st,c) = server.select(mailbox)
161) if st!='OK':
162) logging.error('Error selecting %s mailbox : %s' % (mailbox,st))
163) return
164) (st,ex) = server.expunge()
165) if st=='OK':
166) if len(ex)==1 and ex[0] is None:
167) logging.info('Mailbox %s expunged, no mail removed' % mailbox)
168) else:
169) logging.info('Mailbox %s expunged : %s mail(s) removed' % (mailbox,len(ex)))
170) else:
171) logging.info('Error expunging %s mailbox, return "%s"' % (mailbox,st))
172) except Exception as e:
173) logging.error('Error expunging %s mailbox : %s' % (mailbox,e))
174)
175)
|