python 发送邮件
126或163邮箱需要登录发送邮件的邮箱--设置--开启客户端授权密码后才能正常发送邮件
#!/usr/bin/python3 import smtplib from email.mime.text import MIMEText import time def sendmsg(data): email_host = 'smtp.126.com' #邮箱地址 email_user = 'xxxx@126.com' # 发送者账号 email_pwd = 'xxxx' # 发送者的密码 maillist ='xxxx@qq.com' #收件人邮箱,多个账号的话,用逗号隔开 me = email_user msg = MIMEText(data) # 可直接写邮件内容,也可以是变量,这里的data是变量 msg['Subject'] = 'orderinsert' # 邮件主题 msg['From'] = me # 发送者账号 msg['To'] = maillist # 接收者账号列表 smtp = smtplib.SMTP(email_host,port=25) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25 smtp.login(email_user, email_pwd) # 发送者的邮箱账号,密码 smtp.sendmail(me, maillist, msg.as_string()) # 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串 smtp.quit() # 发送完毕后退出smtp jkwb = './ceshi/msg.txt' f = open(jkwb, "r") data0 = f.read() f.close() while True: f = open(jkwb, "r") data = f.read() f.close if data0 != data: sendmsg(data) data0 = data time.sleep(1)
print ('email send success.')