测试的一些知识

python 发送邮件

2017-10-13  本文已影响3人  井湾村夫

#coding:utf-8

import smtplib

from email.mime.text import MIMEText

from email.header import Header

from email.utils import parseaddr, formataddr

import os

#filepath = 'D:/day.txt'

def _format_addr(s):

name, addr = parseaddr(s)

return formataddr(( \

Header(name, 'utf-8').encode(), \

addr.encode('utf-8') if isinstance(addr, unicode) else addr))

def send_email(filepath):

username = 'shen.haodong@shaxiaoseng.com'

password = 'Shd1992018%'

from_addr = 'shen.haodong@shaxiaoseng.com'

to_addr ='shen.haodong@shaxiaoseng.com'

subject = '1222今日工作内容'

to_add2 = 'zhao.xiangyang@shaxiaoseng.com'

print filepath

with open(filepath,'rb') as pf:

data = pf.read()

#sender = '22'

msg = MIMEText(data,'text','utf-8')

msg['From'] = _format_addr(u'Python爱好者 <%s>' % from_addr)

msg['To'] = _format_addr(u'管理员 <%s>' % to_addr)

msg['Subject'] = Header(u'来自浩栋11的问候……', 'utf-8').encode()

smtp = smtplib.SMTP()

smtp.connect('smtp.mxhichina.com',25)

smtp.set_debuglevel(1)

smtp.login(username, password)

smtp.sendmail(from_addr, [to_addr,to_add2], msg.as_string())

smtp.quit()

send_email('D:/day.txt')

上一篇下一篇

猜你喜欢

热点阅读