The Newsletter

2-3 min read

May 09, 2021

Brief

I was planning to start a mailing list for my blog. I looked at all the different providers, and they were just too expensive for me(TBH anything above 0 was too expensive for me). So, I decided to make one for myself.

Details

Before describing the details of the projects, I think it’s important to give a basic overview of my current blog setup:

  1. Gatsby as the static site generator
  2. Github to host my pages
  3. Netlify for managing DNS, deployment and serving

There are three parts to the newsletter, subscribe(storing the email), posting update(sending the email) and unsubscribe(removing the email). Let’s go over each, one by one.

Subscribe

Before you even click subscribe, you may have noticed that the button is disabled. It gets enabled when you enter a valid email address(this validation happens at the backend too). Once you click the button:

Subscribe

  1. A request to Netlify forms is sent
  2. It triggers a Netlify function, which is a sort of wrapper around the AWS Lambda
  3. This function runs all the backend logic, and once all the verification is complete, it stores the data in the firebase collection
  4. It is where the entire mailing list is stored

Post update

Posting is asynchronous. It’s divided into two parts. First is event creation:

Event Creation

  1. On creation of every new post, build and deployment happens
  2. On deployment, another Netlify function gets triggered
  3. It creates an “event” in another firebase collection
  4. This event contains the post creation commit

The second part is sending the email:

Sending Emails

  1. A script runs on remote 24x7 live “Raspberry Pi”, every day twice
  2. It fetches the data from the “event” collection, and picks up all the events which have the status “created”
  3. For each event, it fetches the commit details from Github
  4. Then for each new valid file, it fetches the file content and creates the email content for each valid file
  5. In the first instance, it runs at 08:00 EST
    1. An email is sent to me, for the sanity check
    2. The event’s status is changed to “ready”
  6. The second instance runs at 09:00 EST
    1. It fetches all the subscribers
    2. Sends email one by one to all of subscribers
    3. It sets the latest post of the user to the current post(So, it’s easy to recover in case of random failure and we do not end up sending two emails to the same user)
    4. The event’s status is changed to “done”

Unsubscribe

Unsubscribe

Each email contains a link that you can use to unsubscribe from the mailing list. When you subscribe, a token is assigned to you at the server. The link in the email redirects you to my site and has this token in the query param. Then, when you click unsubscribe:

  1. The Netlify function is triggered again
  2. It first verifies the token
  3. Then removes you from the mailing list

Queries

A few points which might answer your queries:

  1. I do not write that frequently, so there’s no need for a 24x7 online server.
  2. It’s not the end of the world if the readers get the update a few hours late.
  3. At the time of writing, all the cloud services used(Netlify, Firebase, Lambda, SMTP server) serve my purpose at $0. According to my estimation of the above service’s free plan, I’ll be able to serve 1 million users at $0. So, it may not scale to infinity at $0, but it will scale to the moon.

If you still have questions or have found a problem with the above setup, please contact me. You can find the links in the footer.

Note: There’s one thing that is unique to my emailing set(no other emailing service provides this to my knowledge):

You can directly reply to each email, and we can have a conversation over there.


Hello 👋
Subscribe for wholesome stuff about life, tech, football, physics and everything else.
No spam, and there's unsubscribe link in every message.