Here is how I wrote Python script to wish my colleague Happy Birthday

Suhas More
6 min readFeb 22, 2020

Tasks or Things repeating more than once are worth automating — Linus Torvalds.

A few weeks back [at the time of writing this post], we have invited Aniruddha Bhagwat — one of the influential personalities I have ever met after our co-founder Gautam Rege to uncover various aspects of branding, the importance of storytelling, and how to achieve growth as an individual and/or as a business. During his informative and attentive talk, he mentioned things like Personal Branding, Importance of social presence, How to transition from 1x engineer to 10x engineer and much more...

After listening to him I realized if I have to grow as an individual I need to focus on a few things.

This blog post is one step towards my desire.

Immediately after the insightful talk, I decided to create another python project to boost my python learning. Because I was already spending my weekends learning Python by working on my own projects.

Tweet thread about my last project

The Project

We at Josh Software celebrate everyone’s birthday, work anniversary and all small-large achievements daily.
To let everyone know who’s the birthday boy/girl today or who is celebrating work anniversary today, Our internal app broadcast one email to everyone.

The email we receive on birthday and work anniversary.

I decided to create a python project which will send personalized emails from my own account to my dear colleagues on their special day.

Initially, I thought it is going to be an easy task but as I started spending more and more time on it I realized it is not as easy as it seems from the top. As I dived deeper and deeper I have tackled many problems with Gmail API, authentication, the scope of access permission, finding email form colleagues name and to addon, I was still learning python, although I am NodeJS developer it could have been easier to write NodeJS app to me but since I wanted to learn python since too long I choose python as my choice of language.

Problems

I'm listing down all the problems I faced while implementing an automated python script.
1. Read all the birthday, anniversary celebration mails.
2. Detect who all are celebrating a birthday/anniversary by parsing email.
3. Find out colleague's personal email address which is not in a broadcasted email thread(not even in cc or bcc).
4. Send everyone personal email from my own email address.
5. Shoot myself an email containing status of job.
6. Mark broadcasted email as read email.

Problem 0
Our internal app sends work anniversary, birthday, new blog post notifications, internal event, and all common updates from the same email address. So identifying Work Anniversary and Birthday email programmatically was a challenge. I can filter out emails by reading the subject line and applying some string operations on it but this would not be an optimal solution as I have to read all the unread emails to find out one work anniversary email.
Solution
The solution to this problem was to create a label separate label for each category(Birthday/Work Anniversary).
So I simply created two different filters based on the subject line and mark filtered email under two different labels.

Labels

Now I just have to read mail under those two labels. Reading emails under a particular label is an optimized solution over reading all emails and filtering out programmatically as it saves my efforts and precious time. I just have to read email form those two labels and check if there is any unread email present if yes proceed with next step

Problem 1
I wrote a python script using Gmail API to read emails received under those two labels and which are unread. I have to parse the email and find out whose birthday or anniversary today.
By parsing email I was able to find name of the colleague but the only problem was that on some fine day there might be a situation where more than one colleague is celebrating work anniversary. (~71.84 % probability that two persons celebrating birthday/anniversary on the same day as we are 150+ in count)

Solution
Luckily, our birthday and anniversary emails are also automated(obviously) and they have one specific format due to which I was able to identify and filter out everyone's name by writing simple string operations on Subject Line.

Problem 2
Our internal app sends an email to one specific email address which is an email alias. Email sent on that alias will ensure that email gets delivered in everyone's inbox.
Due to which I was not able to find out my colleague's email address who is celebrating his Birthday by just looking at received email. It is not even in cc or bcc or anywhere in the email.
I have to find out my colleague's email address anyhow to send him personal email.

Solution
After a few days of research, I realized Google Contacts API can rescue me from this situation
I added Contact APIs to the app and wrote a separate package in python using built-in library provided by Google to find out an email address using a colleague's name.

So far I have an email and name of my colleague who is celebrating his/her special day(Birthday/Anniversary).

Now I just have, drop a one-liner to wish them.

Problem 3
I wrote a helper function to create a message and send it to colleagues inbox with a catchy subject line. I tested it, the message got delivered in my friend's email box who celebrated his birthday recently. I ran it again on the next day.
I got a callback from my friend saying how many times you gonna wish me?
wait what??

So, the problem was I was reading unread emails programmatically but forgot to mark them unread due to which on the next day same person is getting email again.

Solution
I wrote another service to mark an email as read on success of wishing email using another Gmail API. But buy the time I realized this problem I lost track of the unique identification number of an email so I have to re-write earlier code to maintain that ID to mark that email as read and things got sorted.

I was excited to see my script in action.

Scrip in action

I got delivered my first automated birthday wish to my colleague's inbox and I got thanking replays as well.

That’s all.
Thanks for reading this journey. I also wrote

Disclaimer: solutions implemented above are totally depending on my own research and R&D, It might happen that there are cleaner and easier ways to solve the problems stated above.

Feedback and suggestions are more than welcome and I would love to try out your solutions if any.

Feel free to get in touch with me on Twitter for suggestions.

--

--