Friday, October 02, 2009

Tidbit: Using GMail as your default email client

I mostly use GMail for email purposes and so it makes sense to make it my default email client. There are many ways to do this on the web. However, most are desktop environment or browser specific. It is nice to have things behave similarly across desktop environments or even installations. So I threw together a quick bash script that would use the default browser that is configured for your system. Here it is.



#!/bin/bash

getopen () {

if [ -x /usr/bin/xdg-open ]
then
export opener="/usr/bin/xdg-open"
elif [ -x /usr/bin/kde-open ]
then
export opener="/usr/bin/kde-open"
elif [ -x /usr/bin/gnome-open ]
then
export opener="/usr/bin/gnome-open"
else
echo "Requires xdg-open, kde-open or gnome-open"
exit 1
fi
}

getemail () {

export address=`echo $1 | sed 's/mailto://'`
export targeturl="https://mail.google.com/mail/?view=cm&fs=1&tf=1&source=mailto&to=$address"
}

getopen
getemail $1
exec $opener $targeturl
exit 0


Save this in a file. I called mine gmailer and saved it to ~/bin. Make sure it is executable and that your system is configured to search ~/bin for executable commands. Now configure your system to open mailto: links with gmailer. That would be gmailer %s in Gnome/XFCE and gmailer %t in KDE4. Cool thing is that gmailer emailaddress@isp.com run from command line or the Ctr+F2 launcher works just as well!


There is a limitation to it. If you are not already logged into GMail or your browser does not automatically log you in, it will not work. It surely can be better but it works just fine for me right now. Obviously, suggestions are most welcome! Enjoy!



[EDIT] Want to do this with Yahoo Mail instead? Click here.

7 comments:

jason said...

Why not just configure Thunderbird to your gmail account, seems alot easier and as long as you have a internet connection your connected.

Shane said...

That could be an option. But I use Firefox with Google Gears installed and offline GMail enabled.

This means I get the full 'GMail exeprience' including GTalk (with history), Labels and search available all in one place... even offline (except for chat of course). Besides, I think the GMail interface is the best email UI I have ever used - web based or local.

Anonymous said...

This was exactly what I was looking for! Thanks! I am going to see if I can figure out how to adapt it to open up a new chrome tab, instead of a separate window. If I do, I will post it here. --pghpete

Anonymous said...

Scratch that last post! If you have the latest chrome (as of post time) and Fedora 13, it automatically opened another tab instead of a new browser instance. Sweet. --pghpete

Shane said...

Thanks for posting back. I tested it with Chrome just now and it opened in a new tab. I thought I had some setting enabled...

Glad it helped you :)

frodeseverin said...
This comment has been removed by the author.
frodeseverin said...

Note to self: Always remember to preview posts.

Just thought I should help out with the problem with not being logged in prior to using the script.

According to
http://forums.bodhilinux.com/index.php?/topic/3063-solved-chromium-mailto-link-doesnt-open-on-thunderbird-xdg-email/page__view__findpost__p__28245
the correct URL format for giving GMail a mailto: link is
https://mail.google.com/mail?extsrc=mailto&url=mailto:link_here

This works like a snap, and I have tested it withoyt being logged in. Sweet.

I changed you script to look like this.

getemail () {

export targeturl="https://mail.google.com/mail?extsrc=mailto&url=$1"

}

Now, my only trouble is to get xdg-open to prefer Chromium over Chrome.

;)Frode