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。


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

  1. #coding=utf-8  
  2. import MySQLdb  
  3.   
  4. import smtplib  
  5. from email.mime.text import MIMEText  
  6. from email.mime.multipart import MIMEMultipart  
  7. import time  
  8.   
  9. def send_email(receiver):  
  10.   
  11.     smtpserver = 'smtp.qq.com'  
  12.     username = '***@qq.com'  
  13.     sender = '****@qq.com'  
  14.     password = '****'  
  15.       
  16.     subject = 'GPS定位追踪-我的Ta你在哪'  
  17.       
  18.     msg=MIMEMultipart('alternative')  
  19.       
  20.     msg['Subject'] = subject  
  21.     msg['From'] = '****@qq.com'  
  22.     msg['To'] = receiver  
  23.       
  24.     #邮件正文  
  25.       
  26.     text="尊敬的用户:\n您好,欢迎使用 GPS定位追踪--我的Ta你在哪 \n如果需要关注对方的位置,您需要让对方安装本软件登录账号,\  
  27.     \n根据Ta的帐号和Ta授权码(如何查看授权码?点击菜单键->设置\n->查看我的授权码即可) \  
  28.     \n您可以获取软件最新版本 下载地址 。\  
  29.     \n你也可以进入软件,点击设置->检查最新版本进行软件的升级。\  
  30.     \n该软件使用的是我们自己的服务器,可以放心使用。\  
  31.     \n如有任何问题请联系:\  
  32.     \n客服QQ:2294454734 \  
  33.     \n该邮件由系统自动发出,勿回复。"  
  34.   
  35.     html=""" 
  36. <div><font size="4"><span style="font-family: Simsun; line-height: normal;"> 
  37. 尊敬的用户:</span><br style="font-family: Simsun; line-height: normal;"> 
  38. <span style="font-family: Simsun; line-height: normal;">    您好,欢迎使用<font color="#ff0000">  
  39. <b>GPS定位追踪--我的Ta你在哪 </b></font></span></font></div><div><font size="4"> 
  40. <span style="font-family: Simsun; line-height: normal;">如果需要关注对方的位置,您</span> 
  41. <span style="font-family: Simsun; line-height: normal;">需要让对方安装本软件登录账号,</span> 
  42. </font></div><div><font size="4"> 
  43. <span style="font-family: Simsun; line-height: normal;">根据<font color="#ff0000"> 
  44. <b>Ta的帐号和Ta授权码</b></font></span></font> 
  45. <span style="font-size: large; font-family: Simsun; line-height: normal;">(如何查看授权码?</span> 
  46. <span style="font-size: large; font-family: Simsun; line-height: normal;">点击菜单键->设置</span></div><div> 
  47. <span style="font-size: large; font-family: Simsun; line-height: normal;">->查看我的授权码即可)</span></div> 
  48. <div><font size="4"><b><span style="font-family: Simsun; line-height: normal;"><br></span></b></font></div> 
  49. <div><font size="4"><b><span style="font-family: Simsun; line-height: normal;">您可以获取软件最新版本 </span> 
  50. <a href="http://121.199.5.19/download/TouchYou.apk" style="font-family: Simsun; line-height: normal;">下载地址</a> 
  51. <span style="font-family: Simsun; line-height: normal;"> 。</span></b></font></div><div> 
  52. <span style="font-family: Simsun; line-height: normal; font-size: large;">你也可以进入软件,点击<font color="#ff0000"> 
  53. <b>设置->检查最新版本</b></font>进行软件的升级。</span></div><div><font size="4"> 
  54. <span style="font-family: Simsun; line-height: normal;">该软件使用的是我们自己的服务器,可以放心使用。</span></font> 
  55. </div><div><font size="4"><span style="font-family: Simsun; line-height: normal;"><br></span></font></div><div> 
  56. <font face="Simsun" size="4"><span style="line-height: normal;">如有任何问题请联系:</span></font></div><div> 
  57. <font face="Simsun" size="4" color="#ff0000"><span style="line-height: normal;"><b>客服QQ:2294454734</b> 
  58. </span></font><br>该邮件由系统自动发出,勿回复。 
  59. </div> 
  60.     """  
  61.     part1=MIMEText(text,'plain',_charset='utf-8')  
  62.     part2=MIMEText(html,'html',_charset='utf-8')  
  63.        
  64.     msg.attach(part1)  
  65.     msg.attach(part2)  
  66.       
  67.       
  68.     #开始发送  
  69.     smtp = smtplib.SMTP()  
  70.     smtp.connect(smtpserver)  
  71.     smtp.login(username, password)  
  72.     try:  
  73.         smtp.sendmail(sender, receiver, msg.as_string())  
  74.         time.sleep(60)#等待一分钟,防止被服务器屏蔽  
  75.     except:  
  76.         print receiver  
  77.         print(' 邮件发送失败!')  
  78.           
  79.     smtp.quit()  
  80.     print(receiver)  
  81.     print(' 邮件发送成功!')  
  82.   
  83.   
  84. #连接  
  85. cxn = MySQLdb.Connect(host = 'localhost', user = 'root', passwd = '**')  
  86.   
  87. #游标  
  88. cur = cxn.cursor()  
  89.   
  90.   
  91. #创建数据库  
  92.   
  93. cur.execute("USE login")  
  94.   
  95.   
  96. #查询  
  97. print cur.execute("SELECT email FROM user  order by id desc")  
  98. for row in cur.fetchall():  
  99.     for mail in row:  
  100.         #send_email(mail)  
  101.         print mail  
  102.   
  103. #关闭  
  104. cur.close()  
  105. cxn.commit()  
  106. cxn.close()  
#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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值