简单的邮件发送功能,可设置定时器,用BlockingScheduler模块,定时发送
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import smtplib
from email.mime.text import MIMEText
import pandas as pd
from apscheduler.schedulers.blocking import BlockingScheduler
import cx_Oracle
import os
import datetime
#解决乱码问题
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
#数据库连接
conn = cx_Oracle.connect('数据库用户名/数据库密码@数据库IP地址:数据库端口号/数据库实例名')
cursor = conn.cursor()
#列一的值
sql_1='XXXXXXXXXXXXXXXXXXXXXXXX'
#列二的值
sql_2='XXXXXXXXXXXXXXXXXXXXXXXX'
#列三的值
sql_3='XXXXXXXXXXXXXXXXXXXXXXXX'
rs1=cursor.execute(sql_1)
rs2=cursor.execute(sql_2)
rs3=cursor.execute(sql_3)
#数据插入
#当前日期
nowTime=datetime.datetime.now().strftime('%Y/%m/%d')#现在
dataframe = pd.DataFrame({'数据生成日期':nowTime.split('.'),
'列一':rs1,
'列二':rs3,
'列三':rs2,
})
cursor.close()
conn.close()
#结果文件生成(末尾追加)
#提前生成数据统计.csv 文件 共有四列,列名分别是:数据生成日期、列一、列二、列三
dataframe.to_csv('C:/Users/22495/Desktop/数据统计.csv', mode='a', index=False, sep=',',header=False)
#邮件发送
smtp = "smtp.qq.com"
sender = 'qq邮箱地址'
receiver = '收件人邮箱地址'
# 授权密码 可自行百度设置方法 是一串英文字符
pwd = 'xxxxxxxxxxxx'
title = "主题"
contents = "正文"
try:
msg = MIMEMultipart()
msg['Subject']=title #主题
msg['From']=sender #发件人
msg['To']=receiver
part_text=MIMEText(contents)
msg.attach(part_text)
part_attach1 = MIMEApplication(open('附件所在路径/附件名.csv','rb').read()) #打开附件
part_attach1.add_header('Content-Disposition','attachment',filename='附件新名.csv') #为附件命名
msg.attach(part_attach1) #添加附件
smtp= smtplib.SMTP(smtp,25)
smtp.login(sender, pwd)
smtp.sendmail(sender, receiver, msg.as_string())
except Exception as e:
pass