Import contacts from Gmail, Yahoo, Hotmail using contacts gem
Installation:
1. gem install contacts
2. OPTIONAL — Not required –> Install acts_as_authenticated plugin
–> http://raveendran.wordpress.com/2007/11/02/acts_as_authenticated-plungin-in-ror/
3. config/environment.rb file –> Add first line –> require ‘contacts’
4. controller:
controller.rb
def import_contacts
end
def import
@users = User.find(params[:id]) #or use — @users = User.find(1)
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 => “email = ‘#{contact[1]}’” )
@users << u
else
@no_users << {:name => contact[0] , :email => contact[1]}
end
end
respond_to do |format|
format.html {render :template => ‘CONTROLLER/_list, :layout => false}
format.xml {render
ml => @contacts.to_xml}
end
end
5. view file :
import_contacts.html.erb
<h2>Welcome to Raveendran’s Blog(<a href=”http://raveendran.wordpress.com”>http://raveendran.wordpress.com</a>) 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>
</select>
<br />
<p><b>Please Enter Your Email Address Below : (Ex. test1@gmail.com)</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:
_list.html.erb
<% 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
October 22, 2009 at 12:04 pm
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),
Thanks,
Sathish Kumar
October 30, 2009 at 4:47 pm
hi im getting connection timed out while fetching contacts from yahoo..however gmail is working fine..