E-mail解析:
包含header和body:
header 包含控制数据:
寄件人、目的地、信息的标题,
body包含信息本身。
下面是邮件程序用到header的几种情况:
1.From header 可以向用户表明邮件的发件人,它也经常被用在客户点击“回复”按钮的时候。新的邮件被发送到Form header中的地址:
2.Reply-To header 可以设置一个回复的替换地址:
3.Subject header 用于显示邮箱摘要
4.Date header 可以被用来按照到达时间分类邮箱;
5.Message-ID 和 In-Reply-To header 可以帮助某些邮件程序实现线索(threading,分层次地排列邮件);
6.MIME header 可以帮助邮件程序以合适的语言、格式来显示邮件,它们也用来处理附件。
一般实例:trad_gen_simple.py
#!/usr/bin/env python #Traditional Message Generation , Simple #Trad_gen_simple.py from email.MIMEText import MIMEText message = ''' Hello, This is a test message form Traditional Message! -- Anonymous ''' msg = MIMEText(message) msg['To'] = '[email protected]' msg['From'] = 'Test Sender <[email protected]>' msg['Subject'] = 'Test Message ,' print msg.as_string()
添加Date和Message-ID header
Date header 一种为E-mail专门设置的格式,email.Utils.formatdate()函数来产生。
为新邮件添加一个Message-ID header,就不和世界其他邮件Message-ID重复,产生函数: email.Utils.make_msgid()
实例: trad_gen_newhdrs.py
#!/usr/bin/env python #Traditional Message Generation with Date and Message-ID #Trad_gen_newhdrs.py from email.MIMEText import MIMEText from email import Utils message = ''' Hello, This is a test message form Traditional Message! -- Anonymous ''' msg = MIMEText(message) msg['To'] = '[email protected]' msg['From'] = 'Test Sender <[email protected]>' msg['Subject'] = 'Test Message ,' msg['Date'] = Utils.formatdate(localtime = 1) msg['Message-ID'] = Utils.make_msgid() print msg.as_string()
这样这个邮件已经可以发送了。
解析传统邮件:
编辑生成文件message.txt,作为邮件要解析的文本。
编辑 trad_parse.py ,作为解析程序。
迭代出邮件信息,检索出必要信息。
解析日期:
从E-mail中解析出日期并不容易,header里有日期标示方法,实际Date header可能无效。尽管有python emai.Utils模块。 parsedate_tz()函数会载入一个日期字符串,希望能返回10个元素的元组,元组9个元素可以传递给time.mktime()第10个指定时区,而mktime()不能接收,所以使用mktime_tz()函数,他可以转换这个特殊的第10元素。
解析程序例子:data_parse.py
#!/usr/bin/env python #Traditional Message Parsing #data_parse.py import sys, email, time from email import Utils def getdate(msg): """Returns the date/time from msg in seconds-since-epoch, if possible. Otherwise,returens None.""" if not 'date' in msg: #No Date header present. return None datehdr = msg['date'].strip() try : print Utils.mktime_tz(Utils.parsedate_tz(datehdr)) return Utils.mktime_tz(Utils.parsedate_tz(datehdr)) except: # Some sort of error occured, like ly because of an invalid date. return None msg = email.message_from_file(sys.stdin) dateval = getdate(msg) if dateval is None: print "No valid date was found." else: print "dateval:" , dateval
MIME概念:
MIME包含多个部分,常规邮件包含header和内容,当你使用MIME多部分邮件的时候,你可以包含如:邮件文字和附件。它可以用不同方式(例如,纯文本和HTML)。
MIME支持不同传输编码,提供内容类型如:text/plainimage/jpeg 可以指定字符集。
MIME是如何工作:
按照一般约定,最基本内容(纯文本邮件,)会出现在最前面,这样没有MIME的程序也可以阅读,Python可以解析树来使用。
添加MIME附件:
为了编写带有附件的邮件,通常来说,您需要下面几个步骤:
1.建立一个MIMEMultipart()对象,设置邮件的header.
2.为邮件内容部分建立一个MIMEText()对象,也把它放到MIMEMultipart()对象中。
3.为每一个附件,建立一个合适MIME对象,也把它放到MIMEMultpart()对象中。
4.调用MIMEMultipart()对象中的as_string()函数来得到作为结果的邮件。
演示程序:mime_gen_basic.py
#!/usr/bin/env python # MIME attachement generation # mime_gen_basic.py from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email import Utils, Encoders import mimetypes, sys def attachment(filename): fd = open(filename,'rb') mimetype, mimeencoding = mimetypes.guess_type(filename) if mimeencoding or (mimetype is None): mimetype = 'application/octet-stream' maintype, subtype = mimetype.split('/') if maintype == 'text': retval = MIMEText(fd.read(),_subtype=subtype) else: retval = MIMEBase(maintype, subtype) retval.set_payload(fd.read()) Encoders.encode_base64(retval) retval.add_header('Content-Disposition','attachment',filename = filename) fd.close() return retval message = ''' Hello, This is a test message form Traditional Message! -- Anonymous ''' msg = MIMEMultipart() msg['To'] = '[email protected]' msg['From'] = 'Test Sender <[email protected]>' msg['Subject'] = 'Test Message ,' msg['Date'] = Utils.formatdate(localtime = 1) msg['Message-ID'] = Utils.make_msgid() body = MIMEText(message,_subtype='plain') msg.attach(body) for filename in sys.argv[1:]: msg.attach(attachment(filename)) print msg.as_string()
SMTP发送邮件:
Python是通过smtplib模块来实现SMTP的,smtplib模块可以使用SMTP的简单任务变得更容易。
简单SMTP程序:smtp_simple.py
SMTP发送带附件的邮件:
mime_stmp.py
#!/usr/bin/env python # MIME attachement generation # mime_gen_basic.py from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email import Utils, Encoders import mimetypes, sys import smtplib mail_server = 'smtp.exmail.qq.com' mail_server_port = 465 from_addr = 'test@x' to_addr = 'x' s=smtplib.SMTP_SSL(mail_server,mail_server_port) def attachment(filename): fd = open(filename,'rb') mimetype, mimeencoding = mimetypes.guess_type(filename) if mimeencoding or (mimetype is None): mimetype = 'application/octet-stream' maintype, subtype = mimetype.split('/') if maintype == 'text': retval = MIMEText(fd.read(),_subtype=subtype) else: retval = MIMEBase(maintype, subtype) retval.set_payload(fd.read()) Encoders.encode_base64(retval) retval.add_header('Content-Disposition','attachment',filename = filename) fd.close() return retval message = ''' Hello, This is a test message form Traditional Message!
POP3接收邮件:
连接和认证一个远程服务器的过程:
1.建立一个POP3对象,传给它远程服务器的主机名和端口号。
2.调用user()和pass_()函数来发送用户名和密码。
3.如果产生poplib.error_proto异常,登录就失败,服务器就会发送和异常有关的字符串和解释文字。
4.一旦连接上,调用stat(),返回一个一个元组,其中包含了服务器邮箱中邮件的数量和邮件总大小。
5.调用quit()关闭POP连接,代码如下:
一个简单连接: POP3_simple.py
#!/usr/bin/env python #pop3_simple.py import poplib username='test@x' password='x' mail_server = 'xx' p = poplib.POP3(mail_server) p.user(username) p.pass_(password) status = p.stat() print "Mailbox has %d messages for a total of %d bytes" %(status[0],status[1]) p.quit() #for msg_id in p.list()[1]: # print msg_id # outf = open ('%s.eml' % msg_id , 'w') # outf.write('\n' .join(p.retr(msg_id)[1])) # outf.close() #p.quit()
取得邮箱信息: pop3_get.py
#!/usr/bin/env python #pop3_get.py import poplib username='test@x' password='x' mail_server = 'pop.exmail.qq.com' p = poplib.POP3(mail_server) p.user(username) p.pass_(password) status = p.stat() print "Mailbox has %d messages for a total of %d bytes" %(status[0],status[1]) for item in p.list()[1]: print item print item.split(' ') number, octets = item.split(' ') print "Message %s: %s bytes " %(number,octets) p.quit() #for msg_id in p.list()[1]: # print msg_id # outf = open ('%s.eml' % msg_id , 'w') # outf.write('\n' .join(p.retr(msg_id)[1])) # outf.close() #p.quit()
下载邮件:pop3_download.py
#!/usr/bin/env python #pop3_download.py import poplib import getpass,sys,email username='test@x' password='x' mail_server = 'pop.exmail.qq.com' dest = "testmbox" #destfd = open (dest,"at") p = poplib.POP3(mail_server) p.user(username) p.pass_(password) status = p.stat() print "Mailbox has %d messages for a total of %d bytes" %(status[0],status[1]) for item in p.list()[1]: # print item # print item.split(' ') number, octets = item.split(' ') print "Message %s: %s bytes " %(number,octets) # Retrieve the message (storing it in a list of lines) lines = p.retr(number)[1] print lines print "*" * 75 #Create an e-mail object representing the message msg = email.message_from_string("\n" .join(lines)) print msg destfd = open('%s.eml'%number,"at") #Write it out to the mailbox destfd.write(msg.as_string(unixfrom=1)) # Make sure there's an extra newline separating messages destfd.write("\n") p.quit() destfd.close() #for msg_id in p.list()[1]: # print msg_id # outf = open ('%s.eml' % msg_id , 'w') # outf.write('\n' .join(p.retr(msg_id)[1])) # outf.close() #p.quit()
删除邮件:L