![]() |
|
[Tut] Receive Automated Email Notification on Website Update - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Receive Automated Email Notification on Website Update (/thread-100457.html) |
[Tut] Receive Automated Email Notification on Website Update - xSicKxBot - 12-23-2022 Receive Automated Email Notification on Website Update Rate this post ![]() [b]Project Description[/b] This project is a demonstration of a live project on Upwork. We will replicate the entire process using a different URL. We will check for changes in the website after every hour and if there are changes to its content, we will immediately send an automated email to notify that the website has changes in it. This means, whenever the content in the website gets modified it means that there will be changes in it and that is what we capture and then send the notification that there have been changes made to the website. Here’s a quick look at the original project listed on Upwork – ![]() The project can be roughly divided into two major sections –
Step 1: Detecting Changes in the Website We will be following these steps to detect if there were changes in the website or not –
[b]1.1 Import the Necessary Libraries[/b] We will need the help of certain libraries that will aid us in accomplishing our task. Hence, go ahead and import these libraries into your script. Code: import time
import hashlib
from urllib.request import urlopen, Request
import smtplib
import ssl
from email.message import EmailMessage
Code: url = Request('https://news.ycombinator.com/', headers={'User-Agent': 'Mozilla/5.0'})
response = urlopen(url).read()
currentHash = hashlib.sha224(response).hexdigest()
print("running")
PrevVersion = ""
time.sleep(10)
while True: try: time.sleep(30) response = urlopen(url).read() newHash = hashlib.sha224(response).hexdigest() if newHash == currentHash: continue else: print("something changed") response = urlopen(url).read() currentHash = hashlib.sha224(response).hexdigest() time.sleep(30) continue except Exception as e: print(e)As soon as a change is detected, you can send an automated email to the recipient.
Code: Code: # Sending Email Notification
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "work.demo.test.email@gmail.com" # Enter your address
receiver_email = "shubhamsayon@gmail.com" # Enter receiver address
password = 'xxxxxxx'
subject = 'Website Change Notification'
message = """
Subject: Hi there!
Changes have been applied to Website! """
em = EmailMessage()
em['From'] = sender_email
em['To'] = receiver_email
em['Subject'] = subject
em.set_content(message)
context = ssl.create_default_context() with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: server.login(sender_email, password) server.sendmail(sender_email, receiver_email, em.as_string())Caution: You might get an error while sending the email. In the past, we could connect to Gmail easily using Python just by turning on the “Less secure app access” option. But that option is no longer available. Instead, what we have to do now is turn on 2-step verification and then retrieve a 16-character password provided by Google that can be used to log in to Gmail using Python. To learn more about this, refer to the following article here. Putting It All Together Well! We have actually solved the given problem. All that remains to be done is to join the pieces together. So, here’s how the final code looks: Code: import time
import hashlib
from urllib.request import urlopen, Request
import smtplib
import ssl
from email.message import EmailMessage url = Request('https://news.ycombinator.com/', headers={'User-Agent': 'Mozilla/5.0'})
response = urlopen(url).read()
currentHash = hashlib.sha224(response).hexdigest()
print("running")
PrevVersion = ""
time.sleep(10)
while True: try: time.sleep(30) response = urlopen(url).read() newHash = hashlib.sha224(response).hexdigest() if newHash == currentHash: continue else: print("something changed") response = urlopen(url).read() currentHash = hashlib.sha224(response).hexdigest() time.sleep(30) # Sending Email Notification port = 465 # For SSL smtp_server = "smtp.gmail.com" sender_email = "work.demo.test.email@gmail.com" # Enter your address receiver_email = "shubhamsayon@gmail.com" # Enter receiver address password = 'xxxxxxxxx' subject = 'Website Change Notification' message = """ Subject: Hi there! Changes have been applied to Website! """ em = EmailMessage() em['From'] = sender_email em['To'] = receiver_email em['Subject'] = subject em.set_content(message) context = ssl.create_default_context() with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: server.login(sender_email, password) server.sendmail(sender_email, receiver_email, em.as_string()) continue except Exception as e: print(e)Sending automated emails can be such a superpower, especially if you have to send daily emails of the same type while working. The added advantage of the technique that we learned in this article is how easily you can notify users about certain changes via emails. Not only did we learn how to send automated emails but we also saw how one could easily detect changes occurring in a website. I hope this project added some value to your coding journey. Please subscribe and stay tuned for more interesting solutions and discussions in the future. Happy coding! ![]() Related Read: How to Send Emails in Python? |