from email.header import Header from email.mime.text import MIMEText import smtplib def mail_message(from_address, to_address, password, header, msg, smtp_server='smtp.126.com', port='25'): """ :param from_address: :param to_address: :param password: :param header: :param msg: :param smtp_server: :param port: :return: """ s = smtplib.SMTP(smtp_server, port) # s.set_debuglevel(1) _msg = MIMEText(msg, 'plain', 'utf-8') _msg['from'] = from_address _msg['to'] = to_address _msg['subject'] = Header(header, 'utf-8').encode() s.login(from_address, password) s.sendmail(from_address, [to_address], _msg.as_string()) s.quit()