import smtplib
from email. mime. multipart import MIMEMultipart
from email. mime. text import MIMEText
from email. mime. application import MIMEApplication
import pandas as pd
import pymysql
db_config = {
'host' : '192.168.75.148' ,
'user' : 'seluser' ,
'password' : 'zzz-123-ZZZ' ,
'database' : 'db1' ,
}
smtp_server = 'smtp.qq.com'
port = 587
smtp_server_secure = False
sender = '*********@qq.com'
receiver = '*********@qq.com, *********@qq.com'
password = '*****************'
sql = "SELECT id 编号, rid 房间号, name 名字, love 爱好, addr 地址 FROM table1"
connection = pymysql. connect( ** db_config)
try :
with connection. cursor( ) as cursor:
cursor. execute( sql)
result = cursor. fetchall( )
finally :
connection. close( )
data = [ ]
for row in result:
data. append( list ( row) )
df = pd. DataFrame( data)
field_names = [ description[ 0 ] for description in cursor. description]
df. columns = field_names
html_content = df. to_html( index= False , border= 0 , header= True )
html_head = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Table</title>
<style>
body {
background-color:#CCC; /* 设置背景颜色为浅灰色 */
font-size:12px; /* 设置字体大小为12px */
font-family:"宋体"; /* 设置字体家族为宋体 */
}
table {
margin:5px auto; /* 设置表格的外边距上下为5px,左右为auto,使表格在页面中水平居中 */
width:60%; /* 设置表格的宽度为页面宽度的60% */
padding:5px; /* 设置表格的内边距为5px */
}
th {
background-color:#76B7BC; /* 设置表头单元格的背景颜色为浅蓝色 */
font-size:14px; /* 设置表头单元格的字体大小为14px */
padding:5px; /* 设置表头单元格的内边距为5px */
}
tr:nth-child(even) {
background-color:#E6E6FA; /* 设置偶数行(第2n行)的背景颜色为淡紫色 */
padding:5px 6px; /* 设置偶数行(第2n行)的内边距为5px 6px */
}
tr:nth-child(odd) {
background-color:#FFF; /* 设置奇数行(第2n+1行)的背景颜色为白色 */
padding:5px 6px; /* 设置奇数行(第2n+1行)的内边距为5px 6px */
}
</style>
</head>
<body>'''
html_tail = '''</body>
</html>'''
html_content = html_head + html_content + html_tail
print ( 'content: ' , html_content)
with open ( 'a.html' , 'w' ) as f:
f. write( html_content)
df. to_csv( 'data.csv' , index= False , encoding= 'utf-8' )
df. to_excel( 'data.xlsx' , index= False )
msg = MIMEMultipart( )
msg[ 'From' ] = sender
msg[ 'To' ] = receiver
msg[ 'Subject' ] = 'MySQL查询结果'
body = '这是MySQL查询结果的表格附件。请查收附件中的数据表。\n\n' + html_content
msg. attach( MIMEText( body, 'html' ) )
try :
with open ( 'data.csv' , 'rb' ) as f:
attachment = MIMEApplication( f. read( ) )
attachment. add_header( 'Content-Disposition' , 'attachment' , filename= 'data.csv' )
msg. attach( attachment)
except IOError as e:
print ( "错误: 无法读取CSV文件 - " , e)
try :
with open ( 'data.xlsx' , 'rb' ) as f:
xlsx_attachment = MIMEApplication( f. read( ) )
xlsx_attachment. add_header( 'Content-Disposition' , 'attachment' , filename= 'data.xlsx' )
msg. attach( xlsx_attachment)
except IOError as e:
print ( "错误: 无法读取Excel文件 - " , e)
server = smtplib. SMTP( smtp_server, port)
server. starttls( ) if smtp_server_secure else server. login( sender, password)
try :
server. sendmail( sender, receiver, msg. as_string( ) )
print ( "发送成功" )
except smtplib. SMTPException as e:
print ( "错误: 邮件发送失败 - " , e)
server. quit( )