不…
imaplib是一个非常不错的库,它的imap太难以理解了。
您可能希望检查一下
t == 'OK',但是
data[0][1]可以像我用过的一样工作。
这是我用来提取通过电子邮件收到的签名证书的快速示例,虽然不是防d的,但适合我的目的:
import getpass, os, imaplib, emailfrom OpenSSL.crypto import load_certificate, FILETYPE_PEMdef getMsgs(servername="myimapserverfqdn"): usernm = getpass.getuser() passwd = getpass.getpass() subject = 'Your SSL Certificate' conn = imaplib.IMAP4_SSL(servername) conn.login(usernm,passwd) conn.select('Inbox') typ, data = conn.search(None,'(UNSEEN SUBJECT "%s")' % subject) for num in data[0].split(): typ, data = conn.fetch(num,'(RFC822)') msg = email.message_from_string(data[0][1]) typ, data = conn.store(num,'-FLAGS','\Seen') yield msgdef getAttachment(msg,check): for part in msg.walk(): if part.get_content_type() == 'application/octet-stream': if check(part.get_filename()): return part.get_payload(depre=1)if __name__ == '__main__': for msg in getMsgs(): payload = getAttachment(msg,lambda x: x.endswith('.pem')) if not payload: continue try: cert = load_certificate(FILETYPE_PEM,payload) except: cert = None if cert: cn = cert.get_subject().commonName filename = "%s.pem" % cn if not os.path.exists(filename): open(filename,'w').write(payload) print "Writing to %s" % filename else: print "%s already exists" % filename
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)