Skip to Content
Technical Articles

SCN Automated Email Notification

Introduction

Further to our earlier posts on SCN Question Read and automation of the task using Python here, let us proceed to now automate the notification mechanism so that everyday at a certain time, an automated task now runs in the background,retrieves the latest Questions from SCN, and delivers them to you or a group of recipients of interested people as an email.

Something like the below is what we are trying to achieve:-

Fig 1): Email notification of the SCN Questions

Now, when it comes to sending email there are few traditional ways we are accustomed to.Typical steps in any programming language would be to setup up the SMTP host and port, provide from,to email addresses, insert the subject, body contents and attempt the send functionality.While the same could be attempted here as well, two issues stand out.

  1. We need to provide the user id and password of the from account(while this is ok with test accounts, may not be really advisable in the real time scenarios).
  2. Publicly available email servers like Gmail for example could block this activity for security reasons.

Fig 2): Email usage blocked by Gmail

Hence instead of the traditional way, we would proceed with some Email Services platforms that are available for use. Mailchimp, SendGrid are some examples. SendGrid provides easy integration options for multiple (programming) languages. It uses an API based configuration.

Fine, we proceed in the following manner.

Step 1: Create an API Key from SendGrid.

Step 2: Retrieve and Send the Email with SCN Questions

Step 3: Schedule a task(preferably every day at a certain time) to execute the above tasks.

Ok, let us get started.

Step 1: API Key for Email

We proceed to create an API Key at SendGrid by creating an account to utilize the same. There are plenty of language options to choose from. Of course, we select Python here.

Fig 3): SendGrid account set up for API key

Provide a name and generate a key to be used.

Fig 4): API Key Created for SCN Email Notification

Note down the API Key for use in the actual Python program for mail sending. SendGrid does not make the same visible for security reasons.

Step 2: SCN Email

Once the Email sending functionality is tested fine, we proceed to generate the email content with our topic of interest here, the SCN Questions.

We had the data available as below from the first post here.

Now we proceed to create the email components.

Few libraries we require here as listed below:-

“os” module for handling the environment variables set for SendGrid.

Also we need to install “SendGrid” to utilize the mail functionalities provided.

We start off by creating a subject line with proper date format.

Also provided a place holder for the email content.

In our second ‘for’ loop where we search for Titles in the <div> tags, we include the below to capture them as email content, every time appending the questions to a new line.

Finally create the email content with Subject, From, To, Body etc.

Now, let us try to send the email.

Also add some signature content to beautify the content.

Let us have a quick trial run.

Yes, the Email is being sent to the particular recipient.

Result is the same as in Figure 1 above.

Step 3: Automate the Task

Finally we would automate this task as well.

As it being already covered, we repeat the sequence from Step 2 and 3 earlier here.

Result

While we could have formatted the HTML contents of the email to suit our taste, we have achieved the core functionalities using publicly available Email Service and relevant python libraries.

Full code available on GitRepo at ‘Email Notification

 

Thanks,

Jakes

 

Be the first to leave a comment
You must be Logged on to comment or reply to a post.