import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from time import sleep
def grab_screenshot():
# Path for your downloaded chromedriver.
driverPath = '\\Users\\cyberkeeda\\BrowserAutomation\\chromedriver'
# To activate GUI Mode Uncomment te below two lines
# And comment the below four lines staring from chrome_options to driver
#driver = webdriver.Chrome(executable_path=driverPath)
#driver = webdriver.Chrome(driverPath)
chrome_options = Options()
chrome_options.add_argument("headless")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('window-size=1920,1080')
driver = webdriver.Chrome(options=chrome_options, executable_path=driverPath)
driver.get('https://www.mohfw.gov.in/')
sleep(15)
driver.get_screenshot_as_file("screenshot.png")
driver.close()
driver.quit()
def emailScreenShot():
grab_screenshot()
html_string = """
'<b>Covid-19 Cases </b> <br>
Do and Don't Link :https://www.mohfw.gov.in/pdf/socialdistancingEnglish.pdf <br>
<img src="cid:image1"><br>Regards <br> Automation
"""
sendFrom = 'Covid Update <dontreply-covid@cyberkeeda.com>'
sendTo = 'admin@cyberkeeda.com'
# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Covid-19 Cases Status'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
# We reference the image in the IMG SRC attribute by the ID we give it below
msgText = MIMEText(html_string, 'html')
msgAlternative.attach(msgText)
# This example assumes the image is in the current directory
fp = open('screenshot.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
# Send the email (this example assumes SMTP authentication is required)
try:
smtp = smtplib.SMTP('smtp.cyberkeeda.com')
smtp.sendmail(sendFrom, sendTo, msgRoot.as_string())
smtp.quit()
except:
print("SMTP Error")
emailScreenShot()