python 利用smtp发送邮件,html格式

时间:2024-07-31 21:03:38
def send_mail(to_list, sub, context):#sentmail to the maillist
'''
to_list: 发送给谁
sub: 主题
context: 内容
send_mail("xxx@126.com","sub","context")
'''
#print 'test begins'
mail_host = "smtp.163.com"
mail_user = "admin"
mail_pass = "123456"
mail_postfix="163.com" me = mail_user + "<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(context,"html") #context是邮件内容,这里content就可以是带上html的tag了,
msg['Subject'] = sub      #邮件标题
msg['From'] = me      #邮件发送来自于
msg['To'] = ";".join(to_list)#邮件发送给谁 send_smtp = smtplib.SMTP(mail_host)#连接smtp服务器
send_smtp.login(mail_user, mail_pass)#登陆
# print send_smtp
send_smtp.sendmail(me, to_list, msg.as_string())#发送邮件
send_smtp.close()
return True