Using Gmail on my laptop through the web interface can be quite time
consuming. I use the Linux version of Firefox, and it's not exactly known
around the world for its great speed. Anyways, sometimes I just want to
shoot off a quick email, and it would take just as long to load the Gmail
compose form as it would to type the email content. For these times I wrote
a little vim-python script as follows:
" Make sure the Vim was compiled with +python before loading the script...
if !has("python")
finish
endif
:command! -nargs=? GMSend :call GMailSend("<args>")
function! GMailSend(args)
python << EOF
import vim
to = vim.eval('a:args')
GSend(to)
EOF
endfunction
python << EOF
########### BEGIN USER CONFIG ##########
account = 'MY_GMAIL_ACCOUNT'
password = 'MY_GMAIL_PASSWORD'
########### END USER CONFIG ###########
def GSend(to):
"""
Send the current buffer as a Gmail message to a given user.
"""
import libgmail
subject = vim.current.buffer[0]
body = '\n'.join(vim.current.buffer[2:])
ga = libgmail.GmailAccount(account, password)
try:
ga.login()
except libgmail.GmailLoginFailure:
print "Login failed. (Wrong username/password?)"
gmsg = libgmail.GmailComposedMessage(to, subject, body)
if ga.sendMessage(gmsg):
print "Message sent `%s` successfully." % subject
else:
print "Could not send message."
EOF
It's a really simple script, and it uses Python so it will only work if your
Vim was compiled with the +python option. Of course, you'll also
need to edit the script to put in your own Gmail user name and password
(lines 18 and 19).
Installation
Drop the above script (or download it from here) into your ~/.vim/plugin/ directory. Reload Vim or source the file with :so ~/.vim/gmail.vim.
Usage
It's even easier to use. The first line in the buffer is the email's
subject, and the rest is the body. Once the email is composed in this
fashion, type :GMSend <dest> where <dest> is the email
address that you want to send this message to. If all is well, you'll see a
message at the bottom of your Vim window indicating that all is well, and
that the message was sent successfully.
Hopefully somebody out there will find this useful. The version I am
actually using is a little more complex. I've extended the script to
download messages by folder or label and stick the id, subject, and author
in an active buffer. From there I can select one of the shown messages and
the script will display the body in the same buffer. I've kept the rest to
myself as I'm not real thrilled with the UI part, and I'm not sure it will
work as expected for everyone. If you are interested, leave a comment. I'm
pretty confident that the script posted above (the one that just sends mail)
will work as expected. Enjoy!