Last active
February 1, 2018 08:38
-
-
Save qiwihui/a178cc86fb754072702d1bc79be01941 to your computer and use it in GitHub Desktop.
批量删除腾讯企业邮箱邮件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
# coding: utf-8 | |
import imaplib | |
import time | |
import re | |
# 拆分删除列表,一次性标记删除1000个 | |
# 参考: | |
# 1: http://www.lolizeppelin.com/2016/12/14/python-imap-delete/ | |
# 2. https://pymotw.com/2/imaplib/ | |
def chunks(l, n): | |
# yields successive n-sized chunks from l. | |
for i in xrange(0, len(l), n): | |
yield l[i:i + n] | |
list_response_pattern = re.compile( | |
r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)') | |
def parse_list_response(line): | |
flags, delimiter, mailbox_name = list_response_pattern.match(line).groups() | |
mailbox_name = mailbox_name.strip('"') | |
return (flags, delimiter, mailbox_name) | |
def main(): | |
conn = imaplib.IMAP4_SSL('imap.exmail.qq.com', 993) | |
imaplib.Debug = 1 | |
conn.login("[email protected]", "password") | |
print conn.welcome | |
# 列出邮箱中的文件夹 | |
box_list_info_buffer = conn.list() | |
box_list = box_list_info_buffer[1] | |
for line in box_list: | |
flags, delimiter, mailbox_name = parse_list_response(line) | |
print 'Parsed response:', (flags, delimiter, mailbox_name) | |
# 默认邮箱文件夹 | |
box_name = 'INBOX' | |
# 选择邮箱 | |
box = conn.select(box_name) | |
print(box) | |
# 获取所有邮件的id | |
ttype, data = conn.search(None, 'ALL') | |
data_list = data[0].split() | |
# 将所有邮件标记为删除 | |
for i in list(chunks(data_list, 1000)): | |
ret = conn.store(",".join(i), '+FLAGS', '\\Deleted') | |
# print ret | |
print 'commit delete' | |
_start = time.time() | |
conn.expunge() | |
print time.time() - _start | |
conn.logout() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment