-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendEmail.py
More file actions
69 lines (46 loc) · 1.92 KB
/
Copy pathSendEmail.py
File metadata and controls
69 lines (46 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from email.mime.text import MIMEText
import smtplib
import urllib2
def sendEmail():
password = '*********(密码口令)'
smtp_server = 'smtp.qq.com' # 输入SMTP服务器地址
#获取当前硬件读取的温度文本
currTemperature = readData('/Users/mac/Desktop/tem.txt')
temperatureTur = (currTemperature, 30, 32, 15)
msgContent = '早上好! 当前室内温度为 %s 摄氏度. 室外温度为 %s 摄氏度, 今天最高温度在下午1点为 %s 摄氏度, 最低温度为凌晨5点, 温度为 %s 摄氏度. 今天天气晴朗, 祝您一天好心情!' % temperatureTur
msg = MIMEText(msgContent, 'plain', 'utf-8')
msg['Subject'] = '当前室内温度'
msg['Content-Type'] = 'Text/HTML'
msg['From'] = from_addr
msg['To'] = to_addr
try:
server = smtplib.SMTP_SSL(smtp_server, 465)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
print 'Send Success'
except smtplib.SMTPException, e:
print 'Send Failed, %s' % e
def readData(path):
try:
f = open(path, 'r')
data = f.read()
finally:
if f:
f.close()
return data
def getWeather(url):
# http://api.map.baidu.com/telematics/v3/weather?location=上海&output=JSON&ak=FK9mkfdQsloEngodbFl4FeY3
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
header = {'User-Agent': user_agent}
requ = urllib2.Request(url, headers=header)
responds = urllib2.urlopen(requ)
con = responds.read().decode('utf-8')
print con
#sendEmail()
getWeather('http://api.map.baidu.com/telematics/v3/weather?location=上海&output=JSON&ak=FK9mkfdQsloEngodbFl4FeY3')