How I made Gmail my default email client (on linux)

Posted by & filed under How I solved my problem, In English, Tech.

So there I was. I just signed up for yet another membership in an organisation an I got myself yet another email to check. So I decided to add the 5th email inbox on my Thunderbird (2.0.0.24) desktop email client. But I quickly realized that 5 inboxes and lots of separate calendar feeds was causing Thunderbird a dramatic loadtime and with the GUI and data fetching running in the same thread, this caused the application to hang more than 30+ seconds every time I just wanted to open my email client to check my mail.

So I decided to do something that I should have done a long time ago: Use Google products as primary tools for managing my communications.

In this process I used a fair amount of time on adjusting the products to fit my needs, which included:

  • Adding all my different inboxes on different mailservers. (Using the “check mail using POP3” feature of Gmail).
  • Sending mail through my webhost’s SMTP server, which removed the “send on behalf of …@gmail.com” field on the e-mail header of the messages I sent out.
  • Creating a lot of filters for automatic categorization of my incoming e-mails.
  • Enabled IMAP support, so I could fetch my mail from a (Android) smartphone.
  • Added some lab stuff:
    • Custom date formats: 24-hour clock (14:57) or show dates with the day first (31/12/07).
    • Refresh POP accounts: Fetch messages from your POP accounts on demand by using the refresh link on top of the inbox.

But using a web application as an email client adds some problems. One which I made a solution for.

Clicking a mailto: link on a webpage or pressing your e-mail hotkey on your keyboard doesn’t bring you to your Gmail client. But this I fixed with a small python script (I called it gmail.py).

#!/usr/bin/python
# Author: Kraen Hansen (www.creen.dk)
# Date: February 2011
import sys
import os
import urlparse
 
if __name__ == '__main__':
	command = ""
	if len(sys.argv) == 2 and sys.argv[1] != "":
		argument = urlparse.urlparse(sys.argv[1])
		if(argument.scheme == 'mailto'):
			mailoptionsSplitted = argument.path.replace("?","&").split("&")
			options = {}
			if(len(mailoptionsSplitted) >= 1):
				options['to'] = mailoptionsSplitted[0]
			for option in mailoptionsSplitted:
				temp = option.split("=")
				if(len(temp) == 2):
					options[temp[0]] = temp[1]
			#print options
			command += "google-chrome --app='https://mail.google.com/mail?view=cm&tf=0"
			if 'to' in options.keys():
				command += "&to="+options['to']
			if 'cc' in options.keys():
				command += "&cc="+options['cc']
			if 'bcc' in options.keys():
				command += "&bcc="+options['bcc']
			if 'subject' in options.keys():
				command += "&su="+options['subject']
			if 'body' in options.keys():
				command += "&body="+options['body']
			command += "'";
		else:
			print "Unknown scheme:",argument.scheme
	else:
		command = "google-chrome 'https://mail.google.com/mail/#inbox'"
	os.system(command);

I saved this in my home folder of my linux machine (Linux Mint 8 / Helena with Gnome desktop environment and the Google Chrome webbrowser) and made sure that I had it had permissions for execution. Then I opened my “Preferred Applications” settings from the applications menu, where I specified that I would like to use my new script as my “Mail Reader”. (See the update below, if you cannot find the “Custom” option in the list for Mail Readers)
Now my keyboard pops up Gmail in a chrome browser (you can change this for any preferred browser) if I hit the hotkey on the keyboard and it opens the gmail compose dialog if I click a mailto: link on a webpage. It is worth noticing that the script supports the “to”, “cc”, “bcc”, “subject” and “body” fields of the mailto: querystring.

I hope this gives some inspiration to how you could set up your gmail and please let me know if you have done anything smart on your setup.

Update 22/9:

So I just installed Linux Mint 11 (derived from Ubuntu 11.04) and I wished to reproduce my solution, but sadly the Preferred Applications have been given redesigned, so I didn’t have the Customs option in the dropdown menu.

This is simply because it is removed. So I looked into how to get it there.
I realised that this was controlled by the MimeType value of .desktop elements in the
~/.local/share/applications folder.

So here is how to fix it:

Create a new file called gmail.desktop in the ~/.local/share/applications folder (~ being your /home/[username]/ folder of course).
Past into this file, something like this:

#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon=/home/[username]/gmail_logo.png
Name=Gmail
Exec=/home/[username]/gmail.py %u
Comment=Launch the gmail web application
Categories=Application;Network;Email;
MimeType=x-scheme-handler/mailto;

You have to change two things:

  1. Change the Icon variable to point to an image of the gmail logo. I have created one, with transparent background. You will have to download this and change the Icon variable to point to this.
  2. Change the Exec to option to match the location of your gmail.py script (pasted above). Remember to leave in the %u.

Take notice of the last line, stating the MimeType, as being x-scheme-handler/mailto, this makes it show up as a valid email reader in the preferred applications dialog.


Leave a Reply