# __author__="richard"
# coding=utf-8
#用requests 寫的獲取天氣,使用QQ郵箱進行發(fā)郵件
#注意點:qq郵箱使用授權(quán)碼,需要使用SMTP_SSL加密發(fā)送,message中的to 和from 要是收件人和發(fā)件人,多收件人可以使用分號隔開
#2016年12月12日 10:43:51
from email.mime.text import MIMEText
from email.header import Header
import requests
import re,json
import smtplib
URL = 'http://api.jirengu.com/weather.php?'
def open_url(url):
heads = {"Accept": "text/html,application/xhtml+xml,application/xml;",
"Accept-Encoding": "gzip",
"Accept-Language": "zh-CN,zh;q=0.8",
"Referer": "http://www.douyu.com/",
"User-Agent": " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"
}
r = requests.get(url,heads)
r.encoding='utf-8'
if r.status_code != 200:
print "nothing"
else:
return r.text
def sendmail_test():
wt = json.loads(open_url(URL))
date = wt["date"]
city = wt["results"][0]["currentCity"]
pm = wt["results"][0]["pm25"]
weather = wt["results"][0]["weather_data"][0]["weather"]
temperature = wt["results"][0]["weather_data"][0]["temperature"]
str = "city: %s day:%s pm2.5:%s weather:%s temperature:%s " %(city,date,pm,weather,temperature)
mail_host = "smtp.qq.com"
mail_user = "649807430@qq.com"
mail_pass = "*********" #授權(quán)碼
sender = '649807430@qq.com'
receivers ='lichao0111@163.com;649807430@qq.com'
message = MIMEText(str, 'plain', 'utf-8')
message['From'] = sender
message['To'] = receivers
subject = '廈門天氣預(yù)報 '
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP_SSL(mail_host,"465")
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
print "ok"
except smtplib.SMTPException, e:
print e
if __name__=="__main__":
sendmail_test()
利用python發(fā)QQ郵件
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- #!/usr/bin/python # -*- coding: UTF-8 -*- import os impor...
- PHPMailer的介紹: 優(yōu)點: 1 : 可運行在任何平臺之上 2 : 支持SMTP驗證 3 : 發(fā)送郵時指定多...
- 想起那年初夏,你負氣斟滿一杯烈酒 當(dāng)眾飲下,試探著誰的溫柔 我惶恐卻又掙扎著,伸出了手 你含淚擺手的那瞬 我心飄雪...