#!/usr/bin/pythonpython
# -*- coding: UTF-8 -*-mysql
import MySQLdbsql
import smtplib數據庫
from email.mime.text import MIMEText服務器
from email.header import Headeride
import time測試
date = time.strftime('%Y-%m-%d')fetch
# 打開數據庫鏈接ui
conn = MySQLdb.connect(spa
host='localhost',
port = 3306,
user='root',
passwd='',
db ='test',
charset="utf8",
)
# 使用cursor()方法獲取操做遊標
cur = conn.cursor()
# 使用execute方法執行SQL語句
data = cur.execute("select id,phone,num,name from test.user_info;")
# print data
info = cur.fetchmany(data)
cur.close()
conn.commit()
conn.close()
def dd(info):
s = ""
for i in info:
#print i[0]
#print i[1]
#print i[2]
#print i[3].decode('utf-8')
# 漢字須要 decode 成utf-8
s += '%s;%s;%s;%s#' % ( i[0],i[1],i[2],i[3].decode('utf-8') )
#print s
return s
# print(dd(info))
# print(date)
# 發送郵件
msg_from='123456@QQ.com' #發送方郵箱
passwd='XXXXXX' #填入發送方郵箱的受權碼
msg_to='789@qq.com' #收件人郵箱
subject="python郵件測試 + date "#主題
content= str(dd(info)) #正文
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
try:
s = smtplib.SMTP_SSL("smtp.exmail.qq.com",465)#郵件服務器及端口號
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())
print "發送成功"
except s.SMTPException,e:
print "發送失敗"
finally:
s.quit()
=====================================
系統安裝MySQL-python-1.2.5
下載 :https://pypi.python.org/pypi/MySQL-python
# yum install –y mysql-devel
安裝:
[root@www soft]# unzip MySQL-python-1.2.5.zip
[root@www soft]# cd MySQL-python-1.2.5
[root@www soft]# python setup.py build
[root@www soft]# python setup.py install
測試:
[root@www soft]# python
>>> import MySQLdb
>>>
安裝完畢。