Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Receive Automated Email Notification on Website Update

#1
Receive Automated Email Notification on Website Update

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;996315&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</p></div>
<figure class="wp-block-image size-large is-style-default"><img loading="lazy" decoding="async" width="1024" height="829" src="https://blog.finxter.com/wp-content/uploads/2022/12/Receive-Automated-Email-Notification-on-Website-Update-1024x829.jpg" alt="" class="wp-image-996395" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/Receive-Automated-Email-Notification-on-Website-Update-1024x829.jpg 1024w, https://blog.finxter.com/wp-content/uplo...00x243.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x622.jpg 768w, https://blog.finxter.com/wp-content/uplo...6x1244.jpg 1536w, https://blog.finxter.com/wp-content/uplo...8x1659.jpg 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<h2><strong>Project Description</strong></h2>
<p>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. </p>
<p>Here’s a quick look at the original project listed on Upwork –</p>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="653" height="494" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-300.png" alt="" class="wp-image-996318" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-300.png 653w, https://blog.finxter.com/wp-content/uplo...00x227.png 300w" sizes="(max-width: 653px) 100vw, 653px" /></figure>
<p>The project can be roughly divided into two major sections –</p>
<ul>
<li>Detecting if changes have occurred in the website.</li>
<li>Sending an automated email to notify that there have been changes made to a website. </li>
</ul>
<h2>Step 1: Detecting Changes in the Website</h2>
<p>We will be following these steps to detect if there were changes in the website or not –</p>
<ul>
<li>Read the given URL.</li>
<li>Hash the entire website.</li>
<li>Ensure that you wait for a few seconds and then check the next hash value returned.</li>
<li>If the next hash is different from the previous hash, then that means there have been changes made to the website. </li>
</ul>
<h3><strong>1.1 Import the Necessary Libraries</strong></h3>
<p>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.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import time
import hashlib
from urllib.request import urlopen, Request
import smtplib
import ssl
from email.message import EmailMessage</pre>
<h3><strong>1.2 Detect Changes in Website</strong></h3>
<ul>
<li>Once you have imported the necessary libraries, set up the given URL (https://news.ycombinator.com/) to monitor and send a GET request.</li>
<li>Create the hash of the response received using <code>hashlib.sha224(response).hexdigest()</code> and store it in a variable <code>currentHash</code>. This denotes the current hash value.</li>
<li>Now, use an infinite loop to keep checking the hash of the response received from the website and check the difference between the previous hash and the current hash to detect if any change has been made to the website or not. You can use a sleep time delay of 30 seconds or any duration as per the need. In case anything changes you can move on to step 2.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre>
<h2><strong>Step 2: Sending Automated Email Notification</strong></h2>
<p>As soon as a change is detected, you can send an automated email to the recipient. </p>
<ul>
<li>Import the necessary libraries to send the email (already done above).</li>
<li>Set the email sender and receiver.</li>
<li>Set the subject body of the email.</li>
<li>Add SSL</li>
<li>Login and send the email.</li>
</ul>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Sending Email Notification
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "[email protected]" # Enter your address
receiver_email = "[email protected]" # 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())</pre>
<p><strong>Caution: </strong>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. </p>
<p>To learn more about this, refer to the following <strong><a rel="noreferrer noopener" href="https://towardsdatascience.com/how-to-easily-automate-emails-with-python-8b476045c151" target="_blank">article here</a></strong>.</p>
<h2>Putting It All Together</h2>
<p>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:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 = "[email protected]" # Enter your address receiver_email = "[email protected]" # 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)</pre>
<h2>Conclusion</h2>
<p>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. </p>
<p>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! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p class="has-base-background-color has-background"><strong>Related Read: <a href="https://blog.finxter.com/how-to-send-emails-in-python/" target="_blank" rel="noreferrer noopener">How to Send Emails in Python?</a></strong></p>
</div>


https://www.sickgaming.net/blog/2022/12/...te-update/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016