Fetching Cotacts from Gmail, Yahoo and Hotmail

Hello Guys,

This is another post by me, hope this post will help you, if you are stuck in fetching or grab contacts from Yahoo, Gmail and Hotmail. This is possible using gem ‘contacts’.

So here I am posting my code, I know there are lot of things and code available for this, but this is the exact code which I am using.

If you are running an application or want to create a new go ahead, I am writing a new application here but you can add it in your existing code as well.

open your command or console prompt and go into your application directory, here I am creating new:

rails contact

this will create a particular set of files and directories in your application. Now go into your directory, before start working any further be sure that you have installed ‘contacts’ gem with the latest version i.e 1.0.13, to install ‘contacts’ type :

for linux users : sudo gem install contacts

for windows : gem install contacts

if already have but older version update it : sudo gem update contacts

Now open your environemnt.rb file and below this line write

Rails::Initializer.run do |config|

end

require ‘contacts’

be sure to restart your server.

Now open the controllers folder and create this method in any controller if you have or where you want to display this. In my case I am writing this in my account_controller.rb

def invite_friends
#@user = User.find(params[:id])
end

def import
@users = User.find(params[:id])
begin
@sites = {“gmail”  => Contacts::Gmail, “yahoo” => Contacts::Yahoo, “hotmail” => Contacts::Hotmail}
@contacts = @sites[params[:from]].new(params[:login], params[:password]).contacts
@users , @no_users = [], []
@contacts.each do |contact|
#if u = User.find(:first , :conditions => @users.email = ‘#{contact[1]}’ , :include =>[:user])
if u = User.find(:first , :conditions => “email = ‘#{contact[1]}'” )
@users << u
else
@no_users << {:name => contact[0] , :email => contact[1]}
end
end
respond_to do |format|
format.html {render :template => ‘shared/_contact_list’, :layout => false}
format.xml {render :xml => @contacts.to_xml}
end
end

before creating this two method, just be sure that you are giving the user id from invite_friends method to import method.

Open up your invite_friends.html.erb and paste this code :

<% form_tag :action => ‘import’, :id => @user do %>

<select name=”from” id=”from”>
<option value=””>Select Id</option>
<option value=”gmail”>Gmail</option>
<option value=”yahoo”>Yahoo</option>
<option value=”hotmail”>Hotmail</option>
</select>

<BR />
<p>Please Enter Your Email Address Below : <BR />
<input type=”text” name=”login”></p>
<p>Enter Your Password :<BR />
<input type=”password” name=”password”></p>

<p><h4>Note : we are not going to save your Password anywhere </h4></p>
<p><%= submit_tag ‘Find My Friends’ %>

<% end %>

Now start your server, open up firefox and type the address (in my case it is) : http://localhost:3000/account/invite_friends

Select any service like Yahoo, Hotmail or Gmail give the corresponding username and Password and hit submit.

You will get an error message that missing template, to remove that create a folder ‘shared’ in /app/views/ and create a new file name ‘_contact_list.html.erb’ and paste the below code :

<% for i in @contacts %>
<input type=”checkbox” name=”email[]” id=”email_<%= i %>” value=”<%= i %>” /><%= i %><br>
<% end %>

this will help you to take further actions on fetched email addresses, that’s it. You have done..

If you are facing any problems, leave a comment, I will get back in touch with you, and if your code works, don’t forget to leave a comment.

Thanks to ‘contacts’ and thanks to you all also.

86 thoughts on “Fetching Cotacts from Gmail, Yahoo and Hotmail

  1. How are you getting the Yahoo importer to work on your localhost? It seems that you have to register your website to be trusted, and they don’t let you register localhost.

    Thanks,

    Tony

  2. hey tony I have not registered anywhere… all you need is to install the latest version of contacts in your machine.. the older version of it are giving problems with Yahoo

  3. Sorry.. I mentioned ROR 2.6 instead of ROR 2.1 in above comment..
    Actually, simultaneously I’ working on Python 2.6 Django.

  4. Hi punnet,

    Thanks for the questions. I working & learning ROR 2.1 from last 7 month. Getting gr8 reference for me. You really did a good work.
    I need your help..! Can I fetch contacts other than the Gmail, Yahoo, Hotmail by Contacts gem?

    Can u mail me mail-Id…?

    Thanks
    Ganesh K.
    ganesh.kathare@wwindia.com

  5. Hi Ganesh,

    if you have some doubts ask me here itself, I will answer those..

    for your convenience I have left a comment on this in Ruby on Rails Interview Questions.

    feel free to ask me any doubts if you have.

    Regards 🙂
    Puneet

  6. Thanks puneet,

    Actually I’m working on Social networking project. So, that I’m trying to provide/fetch contacts function from any domain of the user, where he have an account.
    like rediff, zapakmail, etc..

    So, is it possible..?

    Thanks
    – Ganesh K.

  7. Hi congiro and Nirbhab

    I checked this gem with gmail and yahoo, I haven’t checked it with Hotmail as I don’t have an account with it… gimme some time to create that, I will let you know guys soon, where the script goes wrong, by the do send me the error logs if both of u can.

    Regards
    Puneet

  8. Hi Nirbhab,

    Thanks for pointing out that Hotmail has changed its protocol, Even I tried this also today but it was not working for me, I am trying to get in touch with lucas, the guy who has created this contacts gem, may be he has some solution of this..

    I will get back with some output soon.

    Regards
    Puneet

  9. Hi Nirbhab,

    I dropped a mail to lucas the soul of this contacts gem.. but I haven’t got any reply from his side.. apart from that I am also checking Rubyforge daily about any updates on this Gem… Hope he will drop me a mail soon, meanwhile if you find out any clew don’t forget to knock me..

    Regards
    Puneet

  10. i couldnt able to install “gem install contacts” in my system. Iam getting ‘gem_original_require’ error. I searched in the net, they told to install “gem install json_pure” but still iam getting the same error.
    Do you have any solutions, let me know.

    Also i need to get contact from outlook also.
    Do you know how to do this , let me know.

    Thanks in Advance,
    Suresh Ramasamy.

  11. Hi i solved my first issue from your blog “http://puneetitengineer.wordpress.com/2008/12/23/problem-in-installing-contacts-gem-in-windows/”

    Thanks a lot !!!

    Now I need to get contact from outlook also.
    Do you have any idea on this ?

  12. Hi suresh,

    feels great that my post gave you +ve results.. well, about contact from outlook I think this gem will not solve your problem…

    I seriously don’t have any ideas on it, but will try to search around and let you know..

    Just in case if you find any solution don’t forget to leave a comment on my blog..

    Cheers
    Puneet

  13. when i tried to execute your code it giving “uninitialized constant AccountController::Contacts”
    If you know any idea on this

  14. /1.8/rubygems.rb:636:in `report_activate_error’: Could not find RubyGem json (>= 0.4.1) (Gem::LoadError)

    this is the error i am getting while restarting the server after adding contacts in environment.rb

    rails 2.2.2

  15. Hi Umang,

    If you are working on windows there is a mswin-32 gem available, download that and then install it.

    If you are working on linux, you directly need to type: sudo gem install json in terminal.

    Feel free to ask me any doubts if you have.

    Cheers!!
    Puneet Pandey

  16. Now I can able to get his email id’s from gmail… Is it is possible to get all the contact informations say his first name, last name, company details etc from gmail (if the value exists). If yes, how ?

  17. Hi Puneet,

    Thanks for your great guidence and code posted here. I completed very sucessfulextracting Google Contact Details with your contact.

    Thanks for again

    Rafeek 🙂

  18. Hi Puneet,

    Thanks for putting together this gem…I got my app working with your gem and it seems to retrieve contacts for gmail & yahoo.

    However, with yahoo, I’m noticing some blocking inconsistencies..

    1. For one of email accounts in yahoo, it seems to give me a list of contacts (62) while when I log into yahoo, the number of contacts is much greater…(322)

    2. For one of the email accounts in yahoo, it gives me 0 contacts, while this account in yahoo has a lot of contacts (422)

    I’m really stumped by this inconsistency. I really need to get this to work for both gmail and yahoo….Can you let me know whats happening here? On my server log, there is no suspicious output (same log as the one for successful yahoo account contact retrieval)

    I’m hoping you can respond quickly as I’m blocked at the moment….I need my app to be rock solid and consistent…

  19. Btw, for gmail, all results are consistent for all 3-4 gmail accounts I tested this with…only yahoo retrieval results are inconsistent….

  20. Hi Usha,

    Thanks a ton that my script works for you… but your problem seems strange to me.. coz if you are using contacts-1.0.13 it should work fine for yahoo and gmail.. We all tried this..and this is working… if you can wait give me a hour or so, I can give you the solution (if this problem exists)..

    Meanwhile if you are in a hurry.. you can use plaxo.

    Let me know what do you say?

    cheers!!

  21. Hi Puneet,

    I’ve found this a great post. I have done it flawlessly. What I am wondering as of the moment is that. How am I going to send an invitation mail to list of my friends in Yahoo, Google & Hotmail.
    Help me with the code..

    1. Hey Puneet,

      You are really really good hope to get the code soon. Hope you also create a tutorial about blackbook gem. Cos all of the tutorials in other blogs and sites sucks.

  22. Puneet,

    My app needs to integrate with both gmail and yahoo, so yahoo is critical. I have the latest gem and I had no problems installing or getting it to work. But for sure the results in yahoo are not right…

    Can you clarify if the gem is using ClientLogin API from gmail/yahoo or scraping technology (like a contact grabber kind of script..)?

    Thanks,
    Usha

  23. Hi usha,

    Sorry for posting my reply late, I am wondering how it is still not working for you.

    Can I ask you which version of rails you are using?

    As per you doubt is concerned, I would like to tell you that its kind of client login API, if you look at the configuration files, you will find that it has predefined set of variables like:
    URL = “http://mail.yahoo.com/”
    LOGIN_URL = “https://login.yahoo.com/config/login”
    etc etc.

    so you can see it will not pick you to the direct yahoo, gmail websites.

    I think yahoo uses CSV, so please check if you have fastercsv installed in your system.

    Let me know if this helps.

    Cheers!!
    Puneet

  24. @charlie

    This code will send mail to the list of users you have shortlisted.. I am assuming here that you know how to send mail in rails.

    First of all create a form_for syntax like this:
    { :controller => ‘contacts’, :action => ‘add_contact’ } do %>

    <input name="email[]" type="checkbox" id="email_” value=”” />

    Then in your contacts controller write this code:
    def add_contact
    params[:email].each do |email_list|
    @contact = Contact.new(params[:contact])
    @contact.user_id = current_user.id
    @contact.email_id = email_list
    if @contact.save!
    UserMailer::deliver_request_confirmation(@contact, @contact.email_id)
    else
    flash[:error]=”There is some problem in sending mail”
    end
    end
    redirect_to(listing_contact_path)
    flash[:notice] = “Contacts has been saved”
    end

    Work to that accordingly, I am sure this will gonna help you 🙂

    Cheers!!
    Puneet

  25. @charlie

    I haven’t used it, but I have some clue of it, you need to install the plugin for that, by running this command:

    ruby script/plugin install svn://rubyforge.org//var/svn/gravatarplugin/plugins/gravatar

    once the plugin installed, write something like this in front of each user where your email address are:

    or it might be
    or it might be

    depending upon the variable name you are using.

    Try this, Unfortunately I have not used it in my app. You have to create it by your own 🙂

    Cheers!!
    Puneet

  26. Mr. Puneet,

    I got these errors:

    NoMethodError in EmailController#add_contact

    You have a nil object when you didn’t expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.each

    Help,

    Charlie

  27. @charlie

    I think you are a rails beginner.The problem occurs coz you are working on arrays.. make sure you are getting the right results while passing values to the controller.

  28. Puneet – I’m going to test this tonight (see if I have fastercsv and if it makes a difference to the consistency of results from yahoo) and then post my results here as soon as I can…

  29. @charlie

    Anytime 🙂

    @usha

    I looked at this gem configuration, It requires csv, which is a pre-build library of ruby. so you don’t need to go for installing fastercsv gem. I am also creating an app, will let you know once it works..

    but yaa surely I will wait for you reply and do let me know what rails version you are using and which OS you are using?

    Regards
    Puneet

  30. Puneet,

    I seem to have the faster csv already (1.4.0). Here are is my env version information. Again, some yahoo contacts are showing up for some accounts (while 0 contacts for other yahoo accounts although there are close to 437 contacts in that account)…I’ll look forward to your reply. Here is the info you asked…

    OS: Windows XP (Dell laptop)

    Rails version:
    ———-
    Rails 2.0.2

    Ruby version:
    ————-
    ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

    RubyGems Environment:
    ———————–
    – RUBYGEMS VERSION: 1.3.4
    – RUBY VERSION: 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
    – INSTALLATION DIRECTORY: c:/ruby1/lib/ruby/gems/1.8
    – RUBY EXECUTABLE: c:/ruby1/bin/ruby.exe
    – EXECUTABLE DIRECTORY: c:/ruby1/bin
    – RUBYGEMS PLATFORMS:
    – ruby
    – x86-mswin32-60
    – GEM PATHS:
    – c:/ruby1/lib/ruby/gems/1.8
    – C:/Documents and Settings/usha.SUNDEEP-67A019F/.gem/ruby/1.8
    – GEM CONFIGURATION:
    – :update_sources => true
    – :verbose => true
    – :benchmark => false
    – :backtrace => false
    – :bulk_threshold => 1000
    – REMOTE SOURCES:
    http://gems.rubyforge.org/

  31. @usha,

    Please open your yahoo mail from yahoo.com and go into your contact list and see how many contacts you have there, if there are 10 contacts this method will display you only 10 friends who are on that list.

    To debug add a new friend there by giving his/her email and all the essential details and then check this code again, this time I am sure it will show you 11 records(as per my example)

    So the bottom line is this gem never looks how many friends you have, you must have their details saved in the contacts(i.e yahoo’s contacts)

    Cheers!!
    Puneet Pandey

  32. Puneet,

    I have some more insights for you (did some debugging). Wh (when I create new contacts in yahoo for these users, whether after sending emails and confirming to add to my addressbook, OR going to the addressbook directly and adding them there), they DO show up correctly in my app.

    Where I think the issue is retrieval of all existing contacts…Again, new contacts adding in yahoo showing up perfectly…old contacts (only some are showing up while all of them have contact info in the addressbook).

    Ultimately, if yahoo computes the users to show up in the addressbook, I believe the gem should be picking them too. Is there a problem on the yahoo side you think?

    — Usha

  33. Hi Sir,

    I am rails newbie. I’ve followed all of the steps that you include here but it seems the code you gave to the one (Charlie) doesn’t work instead it display errors.

    Please help me,

    Desperate Rails Newbie

  34. Sir,

    You had said to the guy that there are some texts missing on the codes that you had gave to him. I am stuck on sending email.

    Please,

    Desperate Rails Newbie

  35. Oh I do have the code about sending an email. The other detail of the missing texts is what I really want.

    Thanks,
    Desperate Rails Newbie

  36. I am using form _ for syntax in my rhtml page, so follow the steps:
    – put form_for
    – end for_for
    – write @contacts[1] after your check box syntax

    Hope it helps 🙂

  37. Hi Puneet,
    Thanks 🙂 sharing the code. I am able to fetch the contact from gmail and i have list of emails with the name – email of the contact person. These 2 fields are coming from the @contacts then how can i fetch value inside the @users and @nousers in the _contact.html.erb.

    I want only the email from my contacts. If its possible can tell be how can i fetch email only from the gmail contacts.

    Thanks
    Rafeek 🙂

  38. Hi Puneet,

    I’m having a bit of problems importing Hotmail contacts into my rails app. Error I get by calling Contacts::Hotmail.new(‘login’,’password’).contacts is:

    NoMethodError: You have a nil object when you didn’t expect it!
    You might have expected an instance of ActiveRecord::Base.
    The error occurred while evaluating nil.[]
    /Library/Ruby/Gems/1.8/gems/contacts-1.0.13/lib/contacts/hotmail.rb:112:in `contacts’

    Can you take a look at that?

    Wayne

  39. @rafeek,

    You have @contacts variable inside that your name and email id’s are available. If you use @contacts[1], you will get the list of names and if you use @contacts[0] you will get the list of all email ids. Please check it once try to do the opposite of it, coz ur solution might be @contacts[1]

    @wayne

    You are getting this error because Hotmail has changed its protocol and unfortunately contacts-1.0.13 doesn’t support the updated protocol of Hotmail. May be you can ask to lucas directly, I am not sure if the contacts-1.0.14 is up and running.. but I want you to make sure of it.

    Cheers!!
    Puneet Pandey

  40. @usha

    I hope you have got the answer of your problem… I apologise for replying late, I have gone through your post and found this problem something strange. If still I can help you out with something else, don’t feel hesitate to ask me.

    Cheers!!
    Puneet Pandey

  41. Hi Sir,
    Sorry for late replying the code that you gave me gives me this error:

    You have a nil object when you didn’t expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.each

    app/controllers/contacts_controller.rb:5:in `add_contact’

  42. This was the code that I made add_contact.html.erb

    “add_contact”, :controller => “contacts” do %>

             
    <input type="checkbox" name="email_list[]" id="email_” value=”” />

    “submit” %>

  43. Hi Sir,

    I am a first year college student but I don’t follow. Can u please repost it cos I really need the code so bad especially the sending of the email.

    Please sir help me your code works in fetching the contacts but when sending the email it gives me hell errors.

    Please please..

    Glasto

  44. @Desperate Rails Newbie

    You must specify some action also with controller. But Let me know What you have written in your controller’s method.

    @Glasto

    There are several ways of sending mails. You must be having some SMTP settings with you, so that you can send mails. let me know if that settings you have with you.

    Cheers!!
    Puneet

  45. I have tried all the stuff on mailboxes recently, even fetching messages and compose mails too, using ruby’s internal ruby/imap library. It works perfectly with gmail and yahoo, havent checked with others.

    Apart from fetching the contacts, you can add a contact, perform all operations on them mailbox. Contacts gem uses the same library to access the mail box, I just used it because i needed more than only address book, i needed unread messages etc, so I manipulated stuff using this

    http://www.ruby-doc.org/stdlib/libdoc/net/imap/rdoc/index.html

    Hope that is useful

    Cheers

  46. hi

    i m getting connection timed out error while fetching contacts from yahoo..however importing with gmail is working fine..

    plz help..

      1. Hi Puneet,

        Can we import other contact details like first name, last name, profile picture etc. using this gem??

      2. Hi Supriya,

        I am sure there must be some way to fetch the first name, last name. I would recommend you to read the API docs for it. As, I implemented this feature a long back.
        If I come across with anything, will share the details with everyone.

        – Puneet

  47. Hi Puneet,

    I user contacts gem in my ROR application, but the result contacts come along with name and email like test usertestuser@yahoo.com. Is there any way to fetch email only from the contacts?

    If you give a solution it will really help for me.

    Thanks,
    Raj

    1. There is a way to get email addresses only. But, I am not sure what that is.. Probably, you need to google this, or give me sometime, I will post you update on the same.

  48. hi punnet

    I am getting some error like uninitialized constant Contacts:: Gmail while i am impoting contacts from gmail

    1. @lavanya: Its been a long time, I haven’t used this gem. But, are you using it on Rails-3? Or maybe, gmail has changed something in their API.

      But would help you better, if you provide me logs for that.

  49. hi punnet,

    Does it works in rails 3,when i am adding require ‘contacts’ in environment.rb it is showing some error when server starts

    1. Hi Puneet, thanks for your concern. I moved on to something else. If I end up needing to use your gem again I’ll let you know if I run into the same problem. Have a nice day!

  50. hi puneet,
    i am using contacts gem in my ror application i am trying to fetch gmail contacts but its showing undefined method contacts in my controller..

    can you help me why its showing undefined method ‘contacts’ ?

    1. Hi Suresh,

      I will have to check it out.. What’s the status now? Are you able to resolve it? Coz I was not available since last couple of months and got a chance now to see and reply to your comment.

  51. Hi Puneet,

    I have tried this article in my local machine.I got error.

    My Rails version : 4.0.4

    1. rails new contacts

    2. Install the contacts gem

    > gem install contacts

    3. Add this code to gemfile

    gem ‘contacts’, ‘~> 1.2.4′

    4. bundle install

    5. Start server

    > rails s

    I got following error :

    /Users/ganesh/.rvm/gems/ruby-1.9.3-p429/gems/gdata-1.1.1/lib/gdata.rb:21:in `require’: cannot load such file — jcode (LoadError)
    from /Users/ganesh/.rvm/gems/ruby-1.9.3-p429/gems/gdata-1.1.1/lib/gdata.rb:21:in `’
    from /Users/ganesh/.rvm/gems/ruby-1.9.3-p429/gems/contacts-1.2.4/lib/contacts/gmail.rb:1:in `require’
    from /Users/ganesh/.rvm/gems/ruby-1.9.3-p429/gems/contacts-1.2.4/lib/contacts/gmail.rb:1:in `’
    from /Users/ganesh/.rvm/gems/ruby-1.9.3-p429/gems/contacts-1.2.4/lib/contacts.rb:6:in `require’
    from /Users/ganesh/.rvm/gems/ruby-1.9.3-p429/gems/contacts-1.2.4/lib/contacts.rb:6:in `’
    from /Users/ganesh/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `require’
    from /Users/ganesh/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `block (2 levels) in require’

    I made any mistake?

    Please advice.

Leave a comment