#coding=cp936

from email.MIMEText import MIMEText
from email.Header import Header
import smtplib

msg=MIMEText('hello world! 中文邮件测试',_subtype='plain',_charset='gb2312')
msg['subject']=Header('中文邮件名测试test!','gb2312')
msg['from']='test@yahoo.com' #邮件client的所显示的sender,可有也可以
msg['to']='hello@yahoo.com'  #邮件client 所显示的收件人。显示什么可以自己定

fromaddr='test@yahoo.com'
toaddrs=['hell01@yahoo.com','hello2@yahoo.com']

server.smtp('smtp.mail.yahoo.com.cn')  #通过yahoo的免费邮箱发送。
server.login('loginname','password')  #yahoo smtp需要认证
server.sendmail(fromaddr,toaddrs,msg.as_string())


#邮件正文为html
#只需对上面做一点改动
msg=MIMEText('hello world! 中文邮件测试',_subtype='plain',_charset='gb2312')
改为
txt='<html><body>hello world! 中文邮件测试</body></html>'
msg=MIMEText(txt,_subtype='html',_charset='gb2312')

#邮件带附件
 #coding=cp936
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart

from email.Header import Header
import smtplib, base64, sys

msg=MIMEMultipart()  #支持附件
txt1=MIMEText('邮件正文',_subtype='plain',_charset='gb2312')
txt1.replace_header('Content-Transfer-Encoding', 'quoted-printable')  #否则邮件原文看不懂,但并不影响读信
msg.attach(txt1)

txt2=base64.encodestring(open('中文附件名test.rar','rb').read()  #附件base64编码
txt2=MIMEText(txt2)
txt2.replace_header('Content-Type', 'application/octet-stream; name=”%s”' % Header('中文f附件名测试test.rar','gb2312'))
txt2.add_header('Content-Disposition', 'p_w_upload;filename=”%s”' %Header('中文附件名测试test.rar','gb2312') )
msg(attach.txt2)

msg['subject']=Header('中文邮件名测试test!(含附件)','gb2312')

msg['from']='test@yahoo.com'

msg['to']='hello@yahoo.com'



fromaddr='test@yahoo.com'

toaddrs=['hell01@yahoo.com','hello2@yahoo.com']



server.smtp('smtp.mail.yahoo.com.cn') 

server.login('loginname','password')

server.sendmail(fromaddr,toaddrs,msg.as_string())


MIMEText默认的编码是 us-ascii, 需要改成 utf-8
MIMEText('中文', _charset='UTF-8') 或者调用set_charset来修改
from email.MIMEText import MIMEText
#msg = MIMEText('中文', _charset='UTF-8')
msg = MIMEText('中文')
msg.set_charset('UTF-8')
print msg.as_string()
发送html邮件
MIMEText(html, 'html')
发送txt邮件
MIMEText(text, 'plain')

最后脚本如下:

#!/usr/bin/env python
# encoding: utf-8

import smtplib
import os
from email.MIMEText import MIMEText
from email.Header import Header

#huizong=os.system("cat /tmp/tongji_vb.log |sed '1d'|awk '{a+=$2;b+=$3;c+=$4;d+=$5;}END{print a,b,c,d}'")
res=os.popen("cat /tmp/tongji_vb.log |sed '1d'|awk '{a+=$2;b+=$3;c+=$4;d+=$5;}END{print a,b,c,d}'")
a = res.read()
a = a.replace(" " ,"\t\t",1)
a = a.replace(" " ,"\t\t\t",2)
a = "总量  " + "\t\t" + a
f = open("/tmp/tongji_vb.log")
s = f.readline()
filecon = s + "\n" + a + "\n"
s = f.readline()
while s:
    filecon += s + "\n"
    s = f.readline()
print filecon    



#textfile='/tmp/tongji_vb.log'

#fp = open(textfile, 'rb')

msg = MIMEText(filecon,_subtype='plain',_charset='UTF-8')
msg.set_charset('UTF-8')
#fp.close()
COMMASPACE = ', '
to=['litao@staff.sina.com.cn']

msg['subject']=Header('微博数据库每日信息统计','UTF-8')
msg['From'] = 'litao@staff.sina.com.cn'
msg['To'] = COMMASPACE.join(to)

s = smtplib.SMTP('localhost')
s.sendmail('litao@staff.sina.com.cn',to, msg.as_string())
s.quit()