代码:
def send_mail_test(subject, content):
# 句柄
# msg = MIMEMultipart('related')
msg = MIMEText(content, 'html', 'base64')
msg['From'] = Header(u'<%s>' % sender, 'utf-8')
msg['To'] = Header(';'.join(receivers), 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
# 发送邮件
smtp = smtplib.SMTP()
try:
smtp.connect(mail_host, 25) # 25为SMTP端口号,465或者994为SSL协议端口
smtp.login(mail_user, mail_password)
smtp.sendmail(sender, receivers, msg.as_string())
print "发送邮件成功"
except smtplib.SMTPException:
print "Error, 发送邮件失败"
traceback.print_exc()
finally:
smtp.quit()
if __name__ == "__main__":
subject = u'【请阅】测试报告'
# content = u'含测试报告邮件测试'
# attach = scan_dir(SJ_RESULT, 'html')
# send_mail(subject, content, attach=attach)
file_html = scan_dir(SJ_RESULT, 'html')
with open(file_html, 'rb') as fp:
content = fp.read()
send_mail_test(subject, content)