, , , , , ,

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|


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

def import
@users = User.find(params[:id])
@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
@no_users << {:name => contact[0] , :email => contact[1]}
respond_to do |format|
format.html {render :template => ‘shared/_contact_list’, :layout => false}
format.xml {render :xml => @contacts.to_xml}

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>

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