<p>
</p><p>第一步:安装包</p><blockquote><p>pip install configparser</p></blockquote><p>
</p><p>第二步:编码</p><blockquote><p>#coding:utf-8</p><p>'''</p><p>Created on 2019年2月28日</p><p> </p><p>@author: nalnait</p><p>'''</p><p>import configparser</p><p>import os</p><p>import smtplib</p><p>from email.mime.text import MIMEText</p><p>from email.mime.multipart import MIMEMultipart</p><p>from email.header import Header</p><p>import MySQLdb</p><p>import csv</p><p>import codecs</p><p>import datetime</p><p> </p><p># import sys</p><p># reload(sys)</p><p>
</p><p>#python3写法:</p><p>import importlib,sys </p><p>importlib.reload(sys)</p><p>
</p><p># sys.setdefaultencoding('utf8')</p><p> </p><p>datenow = datetime.datetime.now().strftime("%Y%m%d")</p><p> </p><p>def read_mysql_to_csv(srcDbConn, fileName):</p><p> with codecs.open(filename=fileName, mode='w', encoding='GB2312') as f: #encoding='GB2312' 保证写入csv的中文不是乱码</p><p> write = csv.writer(f, dialect='excel')</p><p> srcCursor = srcDbConn.cursor()</p><p># sql = "select id,pid,name,enname,pwd,role from users WHERE ymd = {0} ".format(datenow)</p><p> sql = "select id,pid,name,enname,pwd,role from users " </p><p> count = srcCursor.execute(sql)</p><p> print (count)</p><p> data = srcCursor.fetchall()</p><p> id = 'ID'</p><p> pid = '父ID'</p><p> name = '姓名'</p><p> enname = '英文名字'</p><p> pwd = '密码'</p><p> role = '角色'</p><p> title = (id,pid,name,enname,pwd,role)</p><p> write.writerow(title)</p><p> for row in data:</p><p> print (row)</p><p> write.writerow(row) </p><p> </p><p>def send_mail(fileName):</p><p> #读取配置文件</p><p> #config = ConfigParser.SafeConfigParser()</p><p> config = configparser.ConfigParser()</p><p> batRunningPath = os.path.dirname(os.sys.executable)</p><p> batPath = os.path.dirname(os.sys.path[0])</p><p> if os.path.exists(batPath + "\py_20200227\config.ini"): #配置文件config.ini的路径</p><p> config.read(batPath + "\py_20200227\config.ini")</p><p> else:</p><p> config.read(batRunningPath + "\config.ini")</p><p> </p><p> mail_host = config.get("email", "mail_host")</p><p> receveAddr = config.get("email", "mail_to_list")</p><p> ccAddr = config.get("email", "mail_cc_list")</p><p> </p><p> mail_to_list = receveAddr.split(",") #收件人</p><p> mail_cc_list = ccAddr.split(",") #抄送</p><p> </p><p> mail_user = config.get("email", "mail_user") #发件人</p><p> mail_pass = config.get("email", "mail_pass") #发件人密码</p><p> </p><p> mail_sub = "附件测试" #邮件标题</p><p> mail_content = "" #正文内容</p><p> </p><p> #创建一个带附件的实例</p><p> msg = MIMEMultipart()</p><p> </p><p> #构造附件1,传送当前目录下 test.txt 文件</p><p> att1 = MIMEText(open(fileName, 'rb').read(), 'base64', 'utf-8')</p><p> att1["Content-Type"] = 'application/octet-stream'</p><p> att1["Content-Disposition"] = 'attachment; filename="{0}"'.format(fileName)</p><p> msg.attach(att1)</p><p> </p><p> #邮件正文内容</p><p> msg.attach(MIMEText(mail_content,'plain', 'utf-8'))</p><p> </p><p> msg['Subject'] = Header(mail_sub, 'utf-8')</p><p> msg['From'] = mail_user</p><p> msg['To'] = ",".join(mail_to_list)</p><p> msg['Cc'] = ",".join(mail_cc_list)</p><p> receive = mail_to_list</p><p> receive.extend(mail_cc_list)</p><p> try:</p><p> server = smtplib.SMTP_SSL(mail_host)</p><p> server.login(mail_user,mail_pass)</p><p> server.sendmail(mail_user,receive,msg.as_string())</p><p> server.close()</p><p> except Exception as e:</p><p> print (str(e) ) </p><p> </p><p> </p><p>if name == 'main':</p><p> </p><p> charset = "utf8"</p><p> #源数据库</p><p> db = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='oldboy', charset='utf8')</p><p> fileName = "USERS_{0}.csv".format(datenow)</p><p> </p><p> read_mysql_to_csv(db, fileName)</p><p> send_mail(fileName)</p><p>
</p><p>#coding:utf-8</p><p>'''</p><p>Created on 2019年2月28日</p><p> </p><p>@author: nalnait</p><p>'''</p><p>import configparser</p><p>import os</p><p>import smtplib</p><p>from email.mime.text import MIMEText</p><p>from email.mime.multipart import MIMEMultipart</p><p>from email.header import Header</p><p>import MySQLdb</p><p>import csv</p><p>import codecs</p><p>import datetime</p><p> </p><p># import sys</p><p># reload(sys)</p><p>
</p><p>#python3写法:</p><p>import importlib,sys </p><p>importlib.reload(sys)</p><p>
</p><p># sys.setdefaultencoding('utf8')</p><p> </p><p>datenow = datetime.datetime.now().strftime("%Y%m%d")</p><p> </p><p>def read_mysql_to_csv(srcDbConn, fileName):</p><p> with codecs.open(filename=fileName, mode='w', encoding='GB2312') as f: #encoding='GB2312' 保证写入csv的中文不是乱码</p><p> write = csv.writer(f, dialect='excel')</p><p> srcCursor = srcDbConn.cursor()</p><p> sql = "select id,pid,name,enname,pwd,role from users " </p><p> count = srcCursor.execute(sql)</p><p> print (count)</p><p> data = srcCursor.fetchall()</p><p> id = 'ID'</p><p> pid = '父ID'</p><p> name = '姓名'</p><p> enname = '英文名字'</p><p> pwd = '密码'</p><p> role = '角色'</p><p> title = (id,pid,name,enname,pwd,role)</p><p> write.writerow(title)</p><p> for row in data:</p><p> print (row)</p><p> write.writerow(row) </p><p> </p><p>def send_mail(fileName):</p><p> #读取配置文件</p><p> #config = ConfigParser.SafeConfigParser()</p><p> config = configparser.ConfigParser()</p><p> batRunningPath = os.path.dirname(os.sys.executable)</p><p> batPath = os.path.dirname(os.sys.path[0])</p><p> if os.path.exists(batPath + "\py_20200227\config.ini"): #配置文件config.ini的路径</p><p> config.read(batPath + "\py_20200227\config.ini")</p><p> else:</p><p> config.read(batRunningPath + "\config.ini")</p><p> </p><p> mail_host = config.get("email", "mail_host")</p><p> receveAddr = config.get("email", "mail_to_list")</p><p> ccAddr = config.get("email", "mail_cc_list")</p><p> </p><p> mail_to_list = receveAddr.split(",") #收件人</p><p> mail_cc_list = ccAddr.split(",") #抄送</p><p> </p><p> mail_user = config.get("email", "mail_user") #发件人</p><p> mail_pass = config.get("email", "mail_pass") #发件人密码</p><p> </p><p> mail_sub = "附件测试" #邮件标题</p><p> mail_content = "" #正文内容</p><p> </p><p> #创建一个带附件的实例</p><p> msg = MIMEMultipart()</p><p> </p><p> #构造附件1,传送当前目录下 test.txt 文件</p><p> att1 = MIMEText(open(fileName, 'rb').read(), 'base64', 'utf-8')</p><p> att1["Content-Type"] = 'application/octet-stream'</p><p> att1["Content-Disposition"] = 'attachment; filename="{0}"'.format(fileName)</p><p> msg.attach(att1)</p><p> </p><p> #邮件正文内容</p><p> msg.attach(MIMEText(mail_content,'plain', 'utf-8'))</p><p> </p><p> msg['Subject'] = Header(mail_sub, 'utf-8')</p><p> msg['From'] = mail_user</p><p> msg['To'] = ",".join(mail_to_list)</p><p> msg['Cc'] = ",".join(mail_cc_list)</p><p> receive = mail_to_list</p><p> receive.extend(mail_cc_list)</p><p> try:</p><p> server = smtplib.SMTP_SSL(mail_host)</p><p> server.login(mail_user,mail_pass)</p><p> server.sendmail(mail_user,receive,msg.as_string())</p><p> server.close()</p><p> except Exception as e:</p><p> print (str(e) ) </p><p> </p><p> </p><p>if name == 'main':</p><p> </p><p> charset = "utf8"</p><p> #源数据库</p><p> db = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='oldboy', charset='utf8')</p><p> fileName = "USERS_{0}.csv".format(datenow)</p><p> </p><p> read_mysql_to_csv(db, fileName)</p><p> send_mail(fileName)</p><p>
</p><p>
</p></blockquote><p>
</p><p>INI文件:</p><blockquote><p>[email]</p><p>#smtp address</p><p>mail_host=smtp.163.com:465</p><p>
</p><p>#accept mail</p><p>mail_to_list=email_addrX1@163.com</p><p>mail_cc_list=email_addrX2@163.com</p><p>
</p><p>#send mail</p><p>mail_user=email_addrX3@163.com</p><p>mail_pass=password</p><p>
</p></blockquote><p>第三步,跳过的坑:</p><blockquote><p>import configparser</p><p>config = configparser.ConfigParser()</p><p>python2与python3不同</p></blockquote><blockquote><p>#python2 </p><p># import sys</p><p># reload(sys)</p><p>
</p><p>#python3写法:</p><p>import importlib,sys </p><p>importlib.reload(sys)</p><p>
</p></blockquote><blockquote><p># sys.setdefaultencoding('utf8')</p><p>python2需要,python3就不需要了</p></blockquote><blockquote><p>python2</p><p>except Exception, ex:</p><p>python3</p><p>except Exception as ex:</p><p>
</p></blockquote><p>
</p><p>参考文件:</p><p><a>https://blog.csdn.net/JENREY/article/details/86238541</a></p><p><a>https://www.cnblogs.com/SZxiaochun/p/10396365.html</a></p><p>
</p><p>
</p><p>
</p><p>
</p><p>
</p><p>
</p><p>
</p><p>
</p><p>
</p><p>
</p><p>
</p>
网友评论