#!/usr/bin/python
#coding:utf-8
import urllib
import re
#import MySQLdb
from datetime import *
import smtplib
from email.header import Header
from email.mime.text import MIMEText
import sys
mail_host = 'smtp.163.com'
mail_user = '##########'
mail_pass = '##########'
mail_postfix = '163.com'
def send_mail(to_list,subject,content):
me = mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = me
msg['to'] = to_list
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me,to_list,msg.as_string())
s.close()
return True
except Exception,e:
print str(e)
return False
def Weather(name,pro,city=0):
today = date.today()
if city == 0:
uip = urllib.urlopen('http://qq.ip138.com/weather/%s/index.html' %pro)
else:
uip = urllib.urlopen('http://qq.ip138.com/weather/%s/%s.html' %(pro,city))
fip = uip.read()
rip = re.compile(r"%s(.*)</b><br/>(.*)<br/>(.*)<br/><br/><b>"%today)
result = re.search(rip,fip).group(1)
weath = result.replace('</b>',' ').replace('<br/>',' ').replace('<b>',' ')
s = weath.split(' ')[1:5]
str = '''dear %s:
%s %s %s %s
''' %(name,today,s[0],s[2],s[3])
return str
if __name__ == "__main__":
with open('user.txt') as f:
for i in f.readlines():
name = i.split()[1]
pro = i.split()[2]
11,0-1 Top
if len(i.split()) == 4:
city = i.split()[3]
send_mail(mail,'today weather',Weather(name,pro,city))
else:
send_mail(mail,'today weather',Weather(name,pro))
邮箱地址就不暴露了。
user.txt
[email protected] daidai zhejiang HangZhou
附近是效果图