#!/usr/bin/env pythonTraditional Message Parsingdata_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:" , datevalMIME概念:MIME包含多个部分,常规邮件包含header和内容,当你使用MIME多部分邮件的时候,你可以包含如:邮件文字和附件。它可以用不同方式(例如,纯文本和HTML)。MIME支持不同传输编码,提供内容类型如:text/plainp_w_picpath/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 pythonMIME attachement generationmime_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 p_w_upload(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','p_w_upload',filename = filename)
fd.close()
return retval
message = ''' Hello,
This is a test message form Traditional Message!
-- Anonymous '''
msg = MIMEMultipart()
msg['To'] = 'recipient@example.com'
msg['From'] = 'Test Sender <sender@example.com>'
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(p_w_upload(filename))
print msg.as_string() SMTP发送邮件:Python是通过smtplib模块来实现SMTP的,smtplib模块可以使用SMTP的简单任务变得更容易。简单SMTP程序:smtp_simple.pySMTP发送带附件的邮件:mime_stmp.py
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 pythonpop3_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_idoutf = 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 pythonpop3_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_idoutf = 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 pythonpop3_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 itemprint 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_idoutf = open ('%s.eml' % msg_id , 'w')outf.write('\n' .join(p.retr(msg_id)[1]))outf.close()
#p.quit()