Fetching Cotacts from Gmail, Yahoo and Hotmail

Hey guys, it’s pretty simple to fetch the contacts from gmail, yahoo or hotmail. Just 5-10 minutes is quite enough to be done. Follow the steps.

Step 1 :-

Install the gem contacts (sudo gem install contacts)

Step 2 :-

Add this line in the environment.rb

require ‘contacts’

Step 3 :-

Some coding part in controller on view


def invite_friends

@user = User.find(params[:id])

//Pass the params of user id, can be the current user id also.for eg, invite/invite_friends/1

//It does not have impact of fetching the contacts from gmail, yahoo or hotmail.

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 => “email = ‘#{contact[1]}'” )
@users << u
@no_users << {:name => contact[0] , :email => contact[1]}
respond_to do |format|
format.html {render :template => ‘invite/_contact_list’, :layout => false}
format.xml {render : xml => @contacts.to_xml}

Create a folder named ‘invite’ on the views and create the following erb files.


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


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

That’s it. Done. Any queries let me know.


3 thoughts on “Fetching Cotacts from Gmail, Yahoo and Hotmail”

  1. Hi All,

    Hotmail changed its protocol recently so, we cannot access it from “Contact” gem.

    Just install the below gem and go ahead with the steps mentioned in the above post!

    gem ‘liangzan-contacts’, ‘~>1.2.8’, :require => ‘contacts’

Comments are closed.