January 29, 2010

Gmail Attachment Downloader Script

Filed under: Uncategorized — Tags: , — yasi8h @ 1:54 am

<rant>I wanted to download all the attachments from all the emails in my account, that have a label x on them. With all the cool libs around this should be a piece of cake. But… After a lot of googling, and spending hours installing different versions of the ruby runtimes (found out about RVM in the process, this is very useful), and going through old gems that spits out so many errors when they are run on 1.9 runtimes… etc, came up with this one, that among other things, just work!. One thing that i am again reminded is that its not easy coming back to ruby development when you don’t have a good working environment set up. Installing (it’s more about knowing what dependencies you need for different gems i guess) every thing that is required on Ubuntu is not that simple either.</rant>

How to Run

  • Edit the script to reflect your account and other settings.
  • Install the nesseary gems (gem install tmail)

Things you should know about

  • This was tested on ruby 1.8.7 (2009-06-12 patchlevel 174)
  • You need tmail -> gem install tmail
  • Your Gmail account should be IMAP enabled (more info).
  • The script will first go through all the mails with some label applied on them. And it will download the attachments on these mails one by one. After it have processed one email, it will put on another label on it, to signify that this message’s attachments have been downloaded. So you could continue this script from where it stopped, if the script crashes out in the middle of the process.
  • I first tried doing this using ruby-gmail gem, but for some reason it fails to detect the attachments. But this may be fixed when you are reading this post. So if you want a more simpler script with more gmail oriented functionalities have a look at that project.
  • Whatever you do, do not violate Gmail’s ToS.


  • This process emails one by one, it is not multi threaded. And it will take sometime. So be patient and let it do its job.
  • When a certain email is processed and its attachments are downloaded the script will attach a label to it. This might/might not remove the existing labels from that email. If it does not remove them, you will need to do so manually, if and when you are starting the script to resume its work from where it stopped last time.
  • The script doesn’t know about (gmail) threads, it processes individual emails.

I am giving up, its too hard to post source code on wordpress, get it from git hub

#! /usr/bin/ruby
#This script is baised on

username = ""
password = "pa$sword"
look_in_folder = "gmail_label_of_the_messages_whose_attachments_you_want_downloaded"
move_downloaded_mails_to_folder = "gmail_label_to_be_applied_for_processed_emails"
save_to_folder = "/path/to/folder/where/attachments/will/be/saved"

require 'net/imap'
require 'rubygems'
require 'tmail'

# This is a convenience monkey patch
class Net::IMAP
 def uid_move(uid, mailbox)
 uid_copy(uid, mailbox)
 uid_store(uid, "+FLAGS", [:Deleted])

puts 'Starting...'
imap ='', '993', true)

puts "Logging in as #{username} ..."

imap.login(username, password)
mails = imap.uid_search(["NOT", "DELETED"])

puts "Found #{mails.count} mail(s) in folder '#{look_in_folder}'"

puts "\nFetching the next email (this might take some time depending on the size of the message/attachment..."
mails.each do |uid|
 # save_attachment
 mail = TMail::Mail.parse( imap.uid_fetch(uid, 'RFC822').first.attr['RFC822'] )
 puts "Processing '#{mail.subject}'"
 if ! mail.attachments.blank?
 puts "Detected #{mail.attachments.count} attachment(s)"
 mail.attachments.each {|attachment|
 puts "Saving attachment to '#{attachment.original_filename}'..." + attachment.original_filename,"w+") { |local_file|
 local_file << attachment.gets(nil)


 # archive mail to mailbox
 puts "Moving '#{mail.subject}' to folder '#{move_downloaded_mails_to_folder}'"
 imap.uid_move(uid, move_downloaded_mails_to_folder)

puts "Logging out..."




  1. […] here to read the rest: Gmail Attachment Downloader Script « theKindOfMe Tags: about-contact, and-used, attachments, reason-it-fails, script-with, […]

    Pingback by Gmail Attachment Downloader Script « theKindOfMe | Gmail HOT news — January 29, 2010 @ 1:56 am

  2. […] Gmail Attachment Downloader Script […]

    Pingback by mx’s grotto v5.2.5.1 » email PDF->gmail address->QUE->”label” to “Print”-> Print — July 6, 2011 @ 2:33 pm

  3. This doesn’t work.
    /usr/bin/java: symbol lookup error: /.rvm/gems/jruby-1.6.2/gems/tmail- undefined symbol: rb_get_kcode

    Comment by sudhir — July 28, 2011 @ 4:29 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: