contacts – Ruby gem – Import contacts from Gmail, Yahoo, hotmail

Import contacts from Gmail, Yahoo, Hotmail using contacts gem


1. gem install contacts

2. OPTIONAL — Not required –> Install acts_as_authenticated plugin

3. config/environment.rb file –> Add first line –> require ‘contacts’

4. controller:

def import_contacts


def import
@users = User.find(params[:id]) #or use — @users = User.find(1)
@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 => ‘CONTROLLER/_list, :layout => false}
format.xml {render 😡 ml => @contacts.to_xml}

5. view file :


<h2>Welcome to Raveendran’s Blog(<a href=””></a&gt;) Contact import Page</h2><br />
<b> Please select the domain here: </b><br />
<% 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><b>Please Enter Your Email Address Below : (Ex.</b><BR />
<input type=”text” name=”login”></p>
<p><b>Enter Your Password :</b><br />
<input type=”password” name=”password”></p>
<p><%= submit_tag ‘Fetch Friends’ %>
<% end %>

6. Result page:


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

7. http://localhost:3000/CONTROLLER/import_contacts

8. Enjoy with your old contacts

Note: currently import hotmail contact gives some protocol error. Once the issue fixed in contacts gem I will update here. comments are always welcome


5 thoughts on “contacts – Ruby gem – Import contacts from Gmail, Yahoo, hotmail

  1. Hi Raveendran,

    I have used the contacts gem to import mail accounts contacts for invite friends’ module in my application.

    It was working perfectly; I have tested that before getting into live with Gmail, Yahoo and Hotmail.

    Now, I was re-checking every feature on my site, so tried to import the contacts from Gmail, Yahoo and Hotmail is working perfectly as I tested before, but for Gmail is not working.

    Then I was checking contacts gem what it is throwing the errors. It says,

    C:/Ruby/lib/ruby/gems/1.8/gems/contacts-1.0.16/lib/contacts/gmail.rb:43:in `real_connect’: Username and password do not match (Contacts::AuthenticationError)

    I have passed the correct username and password.

    Is anyone facing the same problem?

    If you have used the contacts gem in your project, please re-check / re-test that and let me know the status for Gmail domain.

    Looking forwarded (suggestion and solutions for this issue),

    Sathish Kumar

  2. i followed this blog. i found one error i.e i menctioned in environment.rb require ‘contacts’.when i using this require ‘contacts’ the server never started. when i replace this require ‘contacts’ the server starting but some syntax errors came in can i import my contacts?please send me a reply

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s