Python学习之邮件发送

####测试环境
Python 2.7.6

####脚本如下
zhangkeyuan@opsnotes:~/Test/smtplib » cat simple2.py

#!/usr/bin/python  
#coding: utf-8  
import smtplib  
from email.mime.text import MIMEText  
HOST = "smtp.qq.com"  
SUBJECT = u"博客流量数据统计"  
TO = "zhangky@jiagouyun.com"  
FROM = "431054426@qq.com"  
msg = MIMEText("""  
     <table width="800" border="0" cellspacing="0" cellpadding="4">  
     <tr>  
        <td bgcolor="#CECFAD" height="20" style="font-size:14px">* 博客数据 <a href="opsnotes.net">更多>></a></td>  
     </tr>  
     <tr>  
        <td bgcolor="#EFEBDE" height="100" style="font-size:13px">  
          1) 日访问量: <font color=red>154333</font> 访问次数:12834 页面浏览:12345 点击数:12306 数据流量:521MB<br>  
          2) 状态码信息<br>  
          &nbsp;&nbsp;500:105 404:3264 503:214<br>  
          3) 访问浏览信息<br>  
          &nbsp;&nbsp;IE:50% firefox:10% chrome:30% other:10%<br>  
          4) 页面信息<br>  
          &nbsp;&nbsp/index.php 42153<br>  
          &nbsp;&nbsp/viwe.php 21451<br>  
          &nbsp;&nbsp/login.php 5112<br>  
          </td>  
     </tr>  
     </table>""","html","utf-8")  

msg['Subject'] = SUBJECT  
msg['From'] = FROM  
msg['To'] = TO  
try:  
         server = smtplib.SMTP()  
         server.connect(HOST, "25")  
         server.starttls()  
         server.login("431054426@qq.com","XXWWXX))000")  
         server.sendmail(FROM, TO, msg.as_string())  
         server.quit()  
         print "邮件发送成功!"  
except Exceotion, e:  
         print "失败: "+str(e)  
zhangkeyuan@opsnotes:~/Test/smtplib »  

####测试发送
zhangkeyuan@opsnotes:~/Test/smtplib » python simple2.py
邮件发送成功!
zhangkeyuan@opsnotes:~/Test/smtplib »

####如图:
Python

OpsNotes wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!