#!/usr/bin/env python #-*- coding:utf-8 -*- #Author: Fonzie import smtplib,string from email.mime.text import MIMEText mail_list = ['test@qq.com'] mail_host = "smtp.qq.com" mail_user = "my_mail_address@qq.com" mail_pass = "you smtp passwd" mail_postfix = "qq.com" def send_mail(to_list,sub,content,subtype="plain"): ''' 函数可以发送邮件 :param to_list: :param sub: :param content: :param subtype: :return: ''' mail_from = "hello<%s@%s>"%(mail_user, mail_postfix) msg = MIMEText(content,_subtype='%s'%subtype,_charset='utf-8') msg['Subject'] = sub msg['From'] = mail_from msg['To'] = ";".join(to_list) try: server = smtplib.SMTP() server.connect(mail_host) server.starttls() #启用加密传输 server.login(mail_user,mail_pass) server.sendmail(mail_from,to_list,msg.as_string()) server.close() return True except: return False if __name__ == '__main__': if send_mail(mail_list,"hello","hello wolrd"): print("send mail successfully") else: print("send mail failurl")