slipfriction.com

Wednesday, 12 September 2018

How to check Gmail using Bash

Sometimes we need to automate things on linux using a gmail account. Using Gmail's built in atom feed, we can use a simple bash script to return all unread emails in XML format. Create an empty file and populate it with the following:

#!/bin/bash
username="USERNAME"
password="PASSWORD"
echo
curl -u $username:$password --silent "https://mail.google.com/mail/feed/atom" |  grep -oPm1 "(?<=<title>)[^<]+" | sed '1d'

Make sure you replace the username and password with your own. If you get no results, check your email for a message stating a sign in attempt was blocked. Open the email and follow the directions.