python 发email
周海汉 /文2010.4.12
同事搭了个postfix邮件服务器,用python测试了一下发邮件:
#!/usr/bin/env python#coding:utf8# Import smtplib for the actual sending functionimport smtplib#第一封邮件# Import the email modules we'll needfrom email.mime.text import MIMEText# Open a plain text file for reading.For this example, assume that# the text file contains only ASCII characters.textfile='sendmail.py.html'fp = open(textfile, 'rb')# Create a text/plain messagemsg = MIMEText(fp.read(),'html','utf8') #这是正确显示Html中文的设置,会解析html标签,不再是原始文本。msg.set_charset('utf8')#这是正确显示中文的设置fp.close()me = 'ablozhou@gmail.com'# the sender's email addressyou = 'zhouhaihan@you.cn'# the recipient's email addressmsg['Subject'] = 'The contents of %s,中文标题' % textfilemsg['From'] = memsg['To'] = you# Send the message via our own SMTP server, but don't include the# envelope header.s = smtplib.SMTP('210.211.225.5')#s.login()s.sendmail(me, , msg.as_string())s.quit()
测试发送成功
更多参考:
http://docs.python.org/library/email-examples.html
页:
[1]