首页 > 代码库 > smtp模块使用

smtp模块使用

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 from bs4 import BeautifulSoup
 4 from urllib.request import urlopen
 5 import time
 6 
 7 def sendMail(subject, body):
 8     msg = MIMEText(body)
 9     msg[Subject] = subject
10     msg[From] = "christmas_alerts@pythonscraping.com"
11     msg[To] = "ryan@pythonscraping.com"
12 
13     s = smtplib.SMTP(localhost)
14     s.send_message(msg)
15     s.quit()
16 
17 bsObj = BeautifulSoup(urlopen("https://isitchristmas.com/"))
18 while(bsObj.find("a", {"id":"answer"}).attrs[title] == "NO"):
19     print("It is not Christmas yet.")
20     time.sleep(3600)
21     bsObj = BeautifulSoup(urlopen("https://isitchristmas.com/"))
22 sendMail("It‘s Christmas!", "According to http://itischristmas.com, it is Christmas!")

利用smtplib来检测网站的变化,然后给自己发邮件

smtp模块使用