How I bootstrapped my way into adding an email subscription feature for my blog.
I had a basic understanding of how emails are sent and what's required for setting up. You have to have access to an SMTP server to send emails. Some sort of form that would collect email addresses of the interested subscribers. Some code that would then take the list of email addresses and send them mails when I post something new.
I thought I'd have to integrate some third party service. I had heard of mailchimp and hoped that I would find some tutorial on how to setup email subscription service for static websites.
I prefer control. I like to have my own things instead of having to fall on some third party.
A bit of research and I found out the horrors of SMTP infrastructure. Turns out, setting up the server is not that hard (still tough for a novice like me), but getting your mails delivered right is tough. I required some advice I could trust.
I went to #dgplug for some recommendations. There are people who have done this before, some of them do it for a living. I wanted to know if its a good idea to dive into self hosting or if they've come across something that could help me.
I completely forgot about gandi. I got two mail boxes when I bought the domain. That means I had a fully configured SMTP server just sitting there waiting for me. I just needed to figure out the rest.
I setup the mailbox - firstname.lastname@example.org and tested it on a couple of email addresses. It worked flawlessly.
Next, I had to come up with an automated way of sending the mails. Python has a convenient library for emails - smtplib . I had never sent mails programmatically before. It was just too easy :) take a look.
But how would this script know that I've posted something new?
I had two choices, either scourge the web looking for how the third party email services do it or think of something myself. Hmm… I chose the later.subscription_hook
The way nikola works is, all the posts are output in a certain folder. What if I could check that folder at a particular time and see if there's any new post in there. Once I confirm that there's a new post, I could just read all the subscriber's emails from a file and send them mails.
os.walk took care of traversing.
os.stat helped in picking out the latest file.
But third party email services won't have access to local file systems. How do they do it?
Turns out, they use RSS feeds. As soon as something comes up in the RSS feed, they use it to send emails to full fledged mailing lists.
I tried going that way, it would certainly save me from checking posts folder time to time but I ran into a problem. My RSS feeds didn't show the category of the post.
I write posts on 3 different categories right now and I want subscribers to be able to choose what kind of posts they want. If the feed won't give me any indication about what kind of post it is, I won't be able to select the relevant subscriber list. So I moved back to the original plan.Form.
Why oh why have we not progressed enough to just imagine our frontend to reality!
I don't like doing forms.
So I turned to the most available and free form service I knew. Google forms. It'll incur periodically downloading an excel file extracting the relevant email addresses and also downloading a bunch of extra libraries to access those files. But totally worth it.
Yeah, I don't like it either. But for now, it'll do. I'll replace it with something better. In time.
I have to add a validation function to check if the extracted email address is as innocent as we're expecting or someone put in something devious instead.
Also, make a form.