import codecs
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import xlrd
import smtplib
f=codecs.open(r'E:test.htm')
d=f.read()
data=xlrd.open_workbook(r'E:test.xlsx')
table=data.sheets()[0]
nrows=table.nrows
ncols=table.ncols
for j in range(ncols):
rp='replace-h'+str(j+1)
d=d.replace(rp,table.cell(1,j).value)
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
from_addr = 'XXXXX@163.com'
password = 'XXXXXX'
to_addr = 'XXXXXX@163.com'
smtp_server = 'smtp.163.com'
msg = MIMEText(d, 'html', 'utf-8')
msg['From'] = _format_addr('666 ' % from_addr)
msg['To'] = _format_addr('管理员 ' % to_addr)
msg['Subject'] = Header('这是一封替换的邮件', 'utf-8').encode()
server = smtplib.SMTP(smtp_server, 25)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()