Python之道--Python连接MYSQL数据库和发送邮件

主机环境:Linux yan-Server 3.4.36-gentoo #3 SMP Mon Apr 1 14:09:12 CST 2013 x86_64 AMD Athlon(tm) X4 750K Quad Core Processor AuthenticAMD GNU/Linux

Python版本:Python 2.7.4

原创作品,转载请标明

http://blog.csdn.net/yming0221/article/details/8965882


Python 2还是选择Python 3?这是挺难抉择的。

Python 2学习起来参考资料比较多,网上的开源的插件对Python 2支持也比Python3好,容易学习。

Python 3改进了Python 2的部分缺点,具体我没有了解。本来想学习Python 3,mysql的Python 3插件一直没有搞定。

虽然Python之父建议:如果你手里还有其他的Python项目,建议你学习Python 2,如果你是个初学者,建议直接学习Python 3。

考虑到虽然版本更新,但是Python的思想没变,我选择了Python 2。


下面的程序的作用是给数据库注册的用户群发通知邮件,直接上代码

#coding=utf-8
import MySQLdb

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import time

def send_email(receiver):

    smtpserver = 'smtp.qq.com'
    username = '***@qq.com'
    sender = '****@qq.com'
    password = '****'
    
    subject = 'GPS定位追踪-我的Ta你在哪'
    
    msg=MIMEMultipart('alternative')
    
    msg['Subject'] = subject
    msg['From'] = '****@qq.com'
    msg['To'] = receiver
    
    #邮件正文
    
    text="尊敬的用户:\n您好,欢迎使用 GPS定位追踪--我的Ta你在哪 \n如果需要关注对方的位置,您需要让对方安装本软件登录账号,\
    \n根据Ta的帐号和Ta授权码(如何查看授权码?点击菜单键->设置\n->查看我的授权码即可) \
    \n您可以获取软件最新版本 下载地址 。\
    \n你也可以进入软件,点击设置->检查最新版本进行软件的升级。\
    \n该软件使用的是我们自己的服务器,可以放心使用。\
    \n如有任何问题请联系:\
    \n客服QQ:2294454734 \
    \n该邮件由系统自动发出,勿回复。"

    html="""
<div><font size="4"><span style="font-family: Simsun; line-height: normal;">
尊敬的用户:</span><br style="font-family: Simsun; line-height: normal;">
<span style="font-family: Simsun; line-height: normal;">    您好,欢迎使用<font color="#ff0000"> 
<b>GPS定位追踪--我的Ta你在哪 </b></font></span></font></div><div><font size="4">
<span style="font-family: Simsun; line-height: normal;">如果需要关注对方的位置,您</span>
<span style="font-family: Simsun; line-height: normal;">需要让对方安装本软件登录账号,</span>
</font></div><div><font size="4">
<span style="font-family: Simsun; line-height: normal;">根据<font color="#ff0000">
<b>Ta的帐号和Ta授权码</b></font></span></font>
<span style="font-size: large; font-family: Simsun; line-height: normal;">(如何查看授权码?</span>
<span style="font-size: large; font-family: Simsun; line-height: normal;">点击菜单键->设置</span></div><div>
<span style="font-size: large; font-family: Simsun; line-height: normal;">->查看我的授权码即可)</span></div>
<div><font size="4"><b><span style="font-family: Simsun; line-height: normal;"><br></span></b></font></div>
<div><font size="4"><b><span style="font-family: Simsun; line-height: normal;">您可以获取软件最新版本 </span>
<a href="http://121.199.5.19/download/TouchYou.apk" style="font-family: Simsun; line-height: normal;">下载地址</a>
<span style="font-family: Simsun; line-height: normal;"> 。</span></b></font></div><div>
<span style="font-family: Simsun; line-height: normal; font-size: large;">你也可以进入软件,点击<font color="#ff0000">
<b>设置->检查最新版本</b></font>进行软件的升级。</span></div><div><font size="4">
<span style="font-family: Simsun; line-height: normal;">该软件使用的是我们自己的服务器,可以放心使用。</span></font>
</div><div><font size="4"><span style="font-family: Simsun; line-height: normal;"><br></span></font></div><div>
<font face="Simsun" size="4"><span style="line-height: normal;">如有任何问题请联系:</span></font></div><div>
<font face="Simsun" size="4" color="#ff0000"><span style="line-height: normal;"><b>客服QQ:2294454734</b>
</span></font><br>该邮件由系统自动发出,勿回复。
</div>
    """
    part1=MIMEText(text,'plain',_charset='utf-8')
    part2=MIMEText(html,'html',_charset='utf-8')
     
    msg.attach(part1)
    msg.attach(part2)
    
    
    #开始发送
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(username, password)
    try:
        smtp.sendmail(sender, receiver, msg.as_string())
        time.sleep(60)#等待一分钟,防止被服务器屏蔽
    except:
        print receiver
        print(' 邮件发送失败!')
        
    smtp.quit()
    print(receiver)
    print(' 邮件发送成功!')


#连接
cxn = MySQLdb.Connect(host = 'localhost', user = 'root', passwd = '**')

#游标
cur = cxn.cursor()


#创建数据库

cur.execute("USE login")


#查询
print cur.execute("SELECT email FROM user  order by id desc")
for row in cur.fetchall():
    for mail in row:
        #send_email(mail)
        print mail

#关闭
cur.close()
cxn.commit()
cxn.close()

也顺便说一下,本人做的一个Android的GPS定位追踪软件,大家有兴趣的可以安装体验一下。下载地址:

http://121.199.5.19/download/TouchYou.apk


posted on 2013-05-23 16:28  yming0221  阅读(629)  评论(0编辑  收藏  举报

导航