Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.
import requests
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def SendEmail():
host = "SMTP-SERVER"
sender = "info@bangtech.com"
receivers = "webmaster@bangtech.com"
subject = "The web site may be down!"
#Establish SMTP Connection
#oSmtp = smtplib.SMTP('smtp.gmail.com', 587)
oSmtp = smtplib.SMTP(host)
#Start TLS based SMTP Session
#oSmtp.starttls()
#Login Using Your Email ID & Password
#oSmtp.login("", "")
#To Create Email Message in Proper Format
msg = MIMEMultipart()
#Setting Email Parameters
msg['From'] = sender
msg['To'] = receivers
msg['Subject'] = subject
#Email Body Content
message = """
Hi All,
Please check the web site
"""
#Add Message To Email Body
msg.attach(MIMEText(message, 'html'))
#To Send the Email
try:
oSmtp.send_message(msg)
print ("Successfully sent email")
except SMTPException:
print ("Error: unable to send email")
#Terminating the SMTP Session
oSmtp.quit()
def MonitoWeb(piUrl, piKeyword):
nCode = 0
resp = requests.get(piUrl)
if resp.ok:
sHtml = resp.text
if piKeyword in sHtml:
print ("We've found the string!")
else:
message = "SOLR may be down!"
print (message)
nCode = -1
else:
print ("Boo! {}".format(resp.status_code))
print (resp.text)
nCode = -1
return nCode
sUrl = 'https://www.bangtech.com'
sKeyword = "bangtech"
resp = MonitoWeb(sUrl, sKeyword)
if resp != 0:
time.sleep(1) # Sleep 1 seconds
resp = MonitoWeb(sUrl, sKeyword)
if resp != 0:
message = "SOLR may be down twice!"
print (message)
SendEmail()