import smtplib from email.mime.text import MIMEText from email.utils import formataddr, parseaddr from email.header import Header def format_addr(s): name, addr = parseaddr(s) return formataddr((Header(name, 'utf-8').encode('utf-8'), addr)) # 发送邮件 from_addr = "1573400842@qq.com" password = " 授权码" smtp_server = 'smtp.qq.com' to_addr = input("To") html = """<html> <body> <div>恭喜你成功离职</div> </body></html>""" msg = MIMEText(html, "html", 'utf-8') msg['From'] = format_addr(u'注册测试<%s>' % from_addr) to_name = input("收件人名称") msg['To'] = format_addr(u'{}<%s>'.format(to_name) % to_addr) msg['Subject'] = Header(u"乖乖贼你妈,乖乖贼你妈", 'utf-8').encode() server = smtplib.SMTP_SSL(smtp_server, 465) server.login(from_addr, password) server.sendmail(from_addr, [to_addr, ], msg.as_string()) server.quit()