jump to navigation

Fetching Cotacts from Gmail, Yahoo and Hotmail September 26, 2008

Posted by Puneet Pandey in Uncategorized.
Tags: , , , , , ,
trackback

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 :x ml => @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.

Comments»

1. Tony - January 23, 2009

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. Puneet Pandey - January 27, 2009

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. Ganesh - February 12, 2009

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

4. Ganesh - February 12, 2009

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. Puneet Pandey - February 12, 2009

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. Ganesh - February 18, 2009

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. Nirbhab - February 24, 2009

Hey,
It works fine, but hotmail says that we need to update the protocol.

Can please suggest what need to be done?

Regards,
Nirbhab

8. congrio - February 25, 2009

I have problems with Hotmail. Does it works for you fetching hotmail contacts?

For me, gmail and yahoo works great.

9. Puneet Pandey - February 25, 2009

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

10. nirbhab - February 26, 2009

Hotmail has changed its protocols, please upgrade this library first. If that does not work, report this error at http://rubyforge.org/forum/?group_id=2693

11. Puneet Pandey - February 26, 2009

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

12. nirbhab - March 2, 2009

Any update you found?

13. Puneet Pandey - March 2, 2009

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

14. suresh ramasamy - March 25, 2009

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.

15. suresh ramasamy - March 25, 2009

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 ?

16. Puneet Pandey - March 25, 2009

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

17. suresh ramasamy - March 25, 2009

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

18. umamageshwari - April 13, 2009

/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

19. Puneet Pandey - April 13, 2009

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

20. suresh ramasamy - April 14, 2009

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 ?

21. Rafeek - June 4, 2009

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 :)

22. Usha P - June 14, 2009

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…

23. Usha P - June 14, 2009

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

24. Puneet Pandey - June 15, 2009

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!!

25. Charlie - June 15, 2009

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..

26. Puneet Pandey - June 15, 2009

Hey Charlie,

I got the problem that you are facing, I have the code of it.. will mail it to u asap.

cheers!!
Puneet

Charlie - June 15, 2009

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.

27. Usha P - June 15, 2009

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

28. Usha P - June 16, 2009

Hi Puneet,

Any updates? Thanks much in advance,

Usha

29. Puneet Pandey - June 16, 2009

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

30. Puneet Pandey - June 16, 2009

if that fastercsv doesn’t work for you, let me know we will test it further.. is hotmail working for you? if you are using contacts-1.0.13

31. Puneet Pandey - June 16, 2009

@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

32. Puneet Pandey - June 16, 2009

@charlie

Some text are missing so work according to that

Charlie - June 16, 2009

Hey Puneet,

I haven’t tried it. But anyways thanks for the code. You’re such a savior for us.

Thanks,
Charlie

33. Charlie - June 16, 2009

For the last time Puneet,

Do you know how to display the avatar of the shortlisted contacts?

Thanks again,
Charlie

34. Puneet Pandey - June 16, 2009

@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

35. Puneet Pandey - June 16, 2009

try this link for more info:
http://gravatarplugin.rubyforge.org/

36. Charlie - June 16, 2009

could you please send a compressed app of this. I am experiencing some bitchy difficulties.

37. Puneet Pandey - June 16, 2009

@charlie

I helped you as much as I can.. now you have all the necessary information and code with you.. dubegging now is on your hand :)

Cheers!!
Puneet

38. Charlie - June 16, 2009

Puneet,

Thanks. :)

…….,

Charlie

39. Charlie - June 16, 2009

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

40. Puneet Pandey - June 16, 2009

@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.

41. Charlie - June 16, 2009

Hey Puneet,

Done.

Thank you very much,

Charlie :)

42. Charlie - June 16, 2009

Hey Puneet,

Done

Thank you very much,

Charlie :)

43. Usha - June 16, 2009

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…

44. Puneet Pandey - June 17, 2009

@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

45. Usha P - June 17, 2009

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/

46. Puneet Pandey - June 18, 2009

@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

47. Usha P - June 18, 2009

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

48. Desperate Rails Newbie - June 22, 2009

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

49. Puneet Pandey - June 22, 2009

I don’t know where are you stuck in your application… let me know your problem exactly so that we would discuss it

Cheers!!

50. Desperate Rails Newbie - June 22, 2009

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

51. Puneet Pandey - June 22, 2009

sending mails is not a one step procedure.. you need -
- action mailer for that
- a mailer model.
- configuration settings(like smtp) in your environment.rb
look at this tutorial for your reference:
http://www.tutorialspoint.com/ruby-on-rails/rails-send-email.htm

Cheers!!

52. Desperate Rails Newbie - June 22, 2009

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

53. Puneet Pandey - June 22, 2009

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 :)

54. Rafeek - June 29, 2009

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 :)

55. Wayne - June 29, 2009

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

56. Puneet Pandey - June 30, 2009

@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

57. Puneet Pandey - June 30, 2009

@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

58. Desperate Rails Newbie - June 30, 2009

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’

59. Desperate Rails Newbie - June 30, 2009

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” %>

60. Glasto - July 3, 2009

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

61. Puneet Pandey - July 3, 2009

@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

62. noisybrain - October 25, 2009

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

63. Anuraj Pandey - October 30, 2009

hi

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

plz help..

Puneet Pandey - October 31, 2009

You need to update your gem:
give this command sudo gem update contacts
It should install the latest version of your gem which is 1.0.18

Hope it helps