Create your own newsletter from any source with python — Part 2 (The Newsletter)

Newsletters can be very annoying sometimes but they can also be useful: Imagine if you can get updates on anything you really care about in a single email that YOU control.

Fabio Magarelli
Analytics Vidhya
3 min readJan 25, 2021

--

Photo by Kristina Tripkovic on Unsplash

Welcome to Part 2 of this 2-part tutorial on how to make your own personalised newsletter with web scraped data from any source. Here we’ll learn how to create and populate an email newsletter template with the data we collected from the web in Part 1 and how to send it through Python and Gmail.

If you’re looking for part 1 of this tutorial, follow the link: https://medium.com/analytics-vidhya/create-your-own-newsletter-from-any-source-with-python-part-1-web-scraping-445e6c9e1d75

The full code is available here: https://github.com/fabiom91/python-newsletter_tutorial

Newsletter Template

First of all we want to check our data and create a template that we will use to make our newsletter. In my case, I chose something like this:

left-image: Photo by Markus Winkler on Unsplash

As you can see I’ve used Bulma to handle the style of my template and make it clean and responsive. You can check out Bulma here: https://bulma.io

Now let’s save our template as “email.html” and create a new Python script or notebook in the same folder where we are going to import the template using BeautifulSoup and we are going to divide the “fixed part” of our template (e.g. html, head, style etc.) from the “dynamic part” (the part of the template that will hold our data such as the “columns”):

Now we can populate the article_template with the data scraped in Part 1 of this tutorial:

Now if we print the email_content, paste it in an HTML file and open it in our browser, this is how it should look like:

Pretty nice eh? 🤩

Setting up your sender email address

All right, we have our email template complete in HTML. Before I show you how to actually send your email through Python, you need to make sure that the email address you want to use to send out the newsletter supports SMTP protocol. Altough Gmail supports SMTP, this is not active by default. To configure SMTP on your sender email address, follow this simple guide on google support page: https://support.google.com/a/answer/176600?hl=en

Sending out the email

Once you have configured your sender email with SMTP, sending your email is very simple:

If everything went well, you should receive your newsletter in a few minutes (time may vary depending on numerous variables).

I hope you enjoyed following through this tutorial. Now you have all the instruments you need to get full control over the contents of your newsletter so that you can receive updates only on what really matters to YOU.

Happy coding! 😃

--

--

Fabio Magarelli
Analytics Vidhya

PhD student at the Centre for Research Training in Artificial Intelligence, University College Cork.