Jesse's Octopress Blog

A blogging framework for hackers.

Show Sender and Subject of Newest Emails Using CheckGmail

If you access the preference window of CheckGmail you may notice that under External Commands a shortcode of %u is used to represent the Gmail web address.

Just below this, there is an input box for running a command when new mail arrives.

My goal was to cause a notification bubble to display info from the new email – specifically the Author and Subject. Unfortunately, there is not a shortcode for these items, in fact the only shortcode available is %u for the Gmail url.

CheckGmail Preferences External Commands

I had never seen any perl code before, and certainly had not written any, so I was surprised when I succeeded at this goal. Perl feels very similar to Ruby to me.

The instructions below show how to add shortcodes to reference the Author (%a) and Subject (%s) of the newest email, for use in the External Commands section of the CheckGmail preferences.

Edit the file /usr/bin/checkgmail

Locate this block of code

/usr/bin/checkgmail
1
2
3
4
5
# allows the number of new messages to be used in scripts! :)
if ($command =~ m/\%m/) {
  my $messages = @messages;
  $command =~ s/\%m/$messages/g;
}

After the above block, add this code

/usr/bin/checkgmail
1
2
3
4
5
6
7
8
9
10
11
# show the newest message sender name
if ($command =~ m/\%a/) {
  my $author_name = $messages[0]{'author'};
  $command =~ s/\%a/$author_name/g;
}

# show the newest message subject
if ($command =~ m/\%s/) {
  my $subject = $messages[0]{'title'};
  $command =~ s/\%s/$subject/g;
}

Now you can use the code %a to display the name of the sender of the most recent mail message.

Use %s for the most recent subject.

For example, I use this command to display a notification with this info

notify-send "New Mail\n From: %a\n%s"

The above code is put in the checkgmail preferences in: Command to execute on new mail. It causes my notification popup to show the author and subject of new mail. I use dunst for notifications.

Comments