使用python获取email的subject中带有指定字符串的邮件
来源:互联网 发布:手机里网络错误怎么办 编辑:程序博客网 时间:2024/06/02 01:28
#! /usr/bin/env# -*- encoding: utf-8 -*-import imaplibimport emailimport argparseimport loggingclass GetEmail(object): def __init__(self, class_args): self.args = class_args self.map4 = None def process(self): self.map4 = imaplib.IMAP4_SSL("pop.gmail.com", 993) self.map4.login(self.args.username, self.args.password) self.map4.select() if self.args.type == 'mo': self.filter_data("hello world", True) elif self.args.type == 'flow': self.filter_data("hello python", False) self.map4.close() self.map4.logout() def filter_data(self, signature, is_mo): typ, data = self.map4.search(None, 'Subject', signature) logger.info("Get %d letters", len(data[0].split())) f = open(self.args.file, "a+") logger.info("The total number of letters:%d", len(data[0].split())) letter_pos = 0 for num in data[0].split(): letter_pos = letter_pos + 1 if letter_pos < self.args.number: continue logger.info("Now,deal letter %d ,the id is %s ", letter_pos, num) typ, data = self.map4.fetch(num, '(RFC822)') messages = email.message_from_string(data[0][1]) subject = messages.get("Subject") h = email.Header.Header(subject) dh = email.Header.decode_header(h) if dh[0][1] is not None: subject = unicode(dh[0][0], dh[0][1]).encode('utf8') else: subject = dh[0][0] j = 0 for part in messages.walk(): j = j + 1 content_type = part.get_content_type() if content_type == 'text/plain': data = part.get_payload(decode=True) content = str(data) if is_mo: beg_pos = content.find('zk_moid_num:') beg_time = subject.rfind(',') + 1 else: beg_pos = content.find('Index Name: nxgnetflow, # of logs:') beg_time = subject.rfind('from') + 4 if beg_pos != -1: end_pos = content.find('\n', beg_pos) f.write('time:%s\t%s\n' % (subject[beg_time:], content[beg_pos:end_pos])) f.flush() f.close()def main(args): get_email = GetEmail(args) get_email.process()if __name__ == "__main__": logging.basicConfig(level=logging.INFO, format='%(name)s %(asctime)s %(levelname)s %(message)s') logger = logging.getLogger("GetEmail") # create the top-level parser parser = argparse.ArgumentParser(prog='Email Receiver') parser.add_argument('-u', '--username', help='the username of email,example -u test', default="test") parser.add_argument('-p', '--password', help='the password of email,example -p test', default="test") parser.add_argument('-f', '--file', help='the path of file,example -f ./tmp.log', default="./tmp.log") parser.add_argument('-t', '--type', help='the type of data that we will get,mo or flow, example: -t mo', default="mo") parser.add_argument('-n', '--number', help='which letter we should start read,example -n 10', type=int,default=1) args = parser.parse_args() main(args)
阅读全文
0 0
- 使用python获取email的subject中带有指定字符串的邮件
- qmail怎么限制用户发邮件的主题subject中带有特定的字符
- 从指定的字符串中提取Email
- 从指定的字符串中提取Email
- python获取指定字符串中重复模式最高的字符串
- 使用python的email、smtplib、poplib模块收发邮件
- 使用 Rx 中预定义的 Subject
- 使用 Rx 中预定义的 Subject
- Python 发送带有中文附件的邮件
- python发送带有附件的邮件
- python 发送带有附件的邮件
- VC 中调用默认邮件发送程序(Outlook)发送 指定附件的email
- c#获取带有汉字的字符串长度
- c#获取带有汉字的字符串长度
- 利用python读取带有中文的字符串,和将带有中文的字符串写到txt文件中
- Python发送邮件——email库、smtplib库的使用
- Python 处理带有 \u 的字符串
- IOS从字符串中获取指定字符之间的字符串
- 解决Spring的声明式事务中的自定义异常不回滚问题
- Java中数组的使用
- CNN的训练图像与测试图像不一致的多尺度问题
- 名字隐藏与重载
- WebRTC学习之ICE深入理解
- 使用python获取email的subject中带有指定字符串的邮件
- leetcode--Balanced Binary Tree
- 深入理解jquery新的绑定事件机制on方法的使用(推荐)
- React常见问题集锦
- xp系统Arduino编译时,出现collect2.exe: error: ld returned 5 exit status的解决方案
- impala的原理架构介绍及应用场景
- log4j配置文件详解
- 剑指Offer:二叉搜索树的第K个结点
- vscode: Visual Studio Code 常用快捷键