hi all,
i want to know how to use gmailer in rails
sudo apt-get install libopenssl-ruby
gem install GMailer
It also req. Python library
You can use as : foo = GMailer.connect(“cpjolicoeur”, “mypassword”)
or
This is procedure :
require ‘gmailer’
GMailer.connect(name,pwd) do |g|
#fetch
g.messages(:label=>“my_label”) do |ml|
puts "Total # of conversations of my_label = " + ml.total.to_s
end
#get contact
g.fetch(:contact=>“freq”).each do |item|
puts “Name: #{item[‘name’]} Email: #{item[‘email’]}”
end
#send message
‘From’ default gmail.com account
g.send(
:to => “[email protected], my_friend@his_company.com,
[email protected]”,
:cc => “[email protected]”,
:subject => “Hello There!”,
:body => “Hi…\n\nBlah blah blah~~…”,
:files => [“./my_pic.jpg”, “./my_cv.txt”])
multiple verified email addresses and choose one ‘From:’ email
address
g.send(
:from => “[email protected]”,
:to => “[email protected], my_friend@his_company.com,
[email protected]”,
:cc => “[email protected]”,
:subject => “Hello There!”,
:body => “Hi…\n\nBlah blah blah~~…”,
:files => [“./my_pic.jpg”, “./my_cv.txt”])
update_preference
g.update_preference(:max_page_size=>50,
:keyboard_shortcuts=>true,
:indicators=>true,
:display_language=>‘en’,
:signature=>‘This is a signature’,
:reply_to=>‘[email protected]’,
:snippets=>true,
:display_name=>‘Display Name’)
get preference
pref = g.preference
puts “Language:#{pref[‘display_language’]}, Page
Size:#{pref[‘max_page_size’]}”
#creating new labels
g.create_label(‘label_name’)
#renaming existing labels
g.rename_label(‘label_name’,‘renamed_label’)
#deleting labels
g.delete_label(‘label_name’)
#applying a label to a message
g.apply_label(msgid,‘label_name’)
#removing a label from a message
g.remove_label(msgid,‘label_name’)
#apply star to a message
g.apply_star(msgid)
#remove star from a message
g.remove_star(msgid)
#archive a message
g.archive(msgid)
#unarchive a message
g.unarchive(msgid)
#mark a message as read
g.mark_read(msgid)
#mark a message as unread
g.mark_unread(msgid)
#report a message as not spam
g.report_spam(msgid)
#report a message as not spam
g.report_not_spam(msgid)
#move a message to trash
g.trash(msgid)
#move a message from trash to inbox
g.untrash(msgid)
#delete a trash message forever
g.delete_trash(msgid)
#delete a spam message forever
g.delete_spam(msgid)
#delete a message forever
g.delete_message(msgid)
#show original message
g.show original(msgid)
#get labels
labels = g.labels
#get messages
g.messages(:label=>labels[0]).each {|m|
puts “Subject: #{m.subject} / Snippet: #{m.snippet}”
}
#get inbox messages
g.messages(:standard=>‘inbox’).each {|m|
puts “Subject: #{m.subject} / Snippet: #{m.snippet}”
}
fetch with filter :read or :star
g.messages(:standard=>‘all’,:read=>false).each {|msg|
# operations on the message
msg.apply_star
msg.apply_label(label)
msg.trash
puts "subject: " + msg.subject
puts "from: " + msg.sender
puts msg.body
puts msg.original
}
end
On Feb 8, 11:37 am, Shweta K. [email protected]
sudo apt-get install libopenssl-ruby
gem install GMailer
It also req. Python library
You can use as : foo = GMailer.connect(“cpjolicoeur”, “mypassword”)
or
This is procedure :
require ‘gmailer’
GMailer.connect(name,pwd) do |g|
#fetch
g.messages(:label=>“my_label”) do |ml|
puts "Total # of conversations of my_label = " + ml.total.to_s
end
#get contact
g.fetch(:contact=>“freq”).each do |item|
puts “Name: #{item[‘name’]} Email: #{item[‘email’]}”
end
#send message
‘From’ default gmail.com account
g.send(
:to => “[email protected], my_friend@his_company.com,
[email protected]”,
:cc => “[email protected]”,
:subject => “Hello There!”,
:body => “Hi…\n\nBlah blah blah~~…”,
:files => [“./my_pic.jpg”, “./my_cv.txt”])
multiple verified email addresses and choose one ‘From:’ email
address
g.send(
:from => “[email protected]”,
:to => “[email protected], my_friend@his_company.com,
[email protected]”,
:cc => “[email protected]”,
:subject => “Hello There!”,
:body => “Hi…\n\nBlah blah blah~~…”,
:files => [“./my_pic.jpg”, “./my_cv.txt”])
update_preference
g.update_preference(:max_page_size=>50,
:keyboard_shortcuts=>true,
:indicators=>true,
:display_language=>‘en’,
:signature=>‘This is a signature’,
:reply_to=>‘[email protected]’,
:snippets=>true,
:display_name=>‘Display Name’)
get preference
pref = g.preference
puts “Language:#{pref[‘display_language’]}, Page
Size:#{pref[‘max_page_size’]}”
#creating new labels
g.create_label(‘label_name’)
#renaming existing labels
g.rename_label(‘label_name’,‘renamed_label’)
#deleting labels
g.delete_label(‘label_name’)
#applying a label to a message
g.apply_label(msgid,‘label_name’)
#removing a label from a message
g.remove_label(msgid,‘label_name’)
#apply star to a message
g.apply_star(msgid)
#remove star from a message
g.remove_star(msgid)
#archive a message
g.archive(msgid)
#unarchive a message
g.unarchive(msgid)
#mark a message as read
g.mark_read(msgid)
#mark a message as unread
g.mark_unread(msgid)
#report a message as not spam
g.report_spam(msgid)
#report a message as not spam
g.report_not_spam(msgid)
#move a message to trash
g.trash(msgid)
#move a message from trash to inbox
g.untrash(msgid)
#delete a trash message forever
g.delete_trash(msgid)
#delete a spam message forever
g.delete_spam(msgid)
#delete a message forever
g.delete_message(msgid)
#show original message
g.show original(msgid)
#get labels
labels = g.labels
#get messages
g.messages(:label=>labels[0]).each {|m|
puts “Subject: #{m.subject} / Snippet: #{m.snippet}”
}
#get inbox messages
g.messages(:standard=>‘inbox’).each {|m|
puts “Subject: #{m.subject} / Snippet: #{m.snippet}”
}
fetch with filter :read or :star
g.messages(:standard=>‘all’,:read=>false).each {|msg|
# operations on the message
msg.apply_star
msg.apply_label(label)
msg.trash
puts "subject: " + msg.subject
puts "from: " + msg.sender
puts msg.body
puts msg.original
}
end
On Feb 8, 11:37 am, Shweta K. [email protected]
I have installed gmailer gem.but i got some error when i tried to
connect
my gmail account.i need to get the inbox messages from my gmail
account.but i can’t.plzz help me.i got “You have a nil object when you
didn’t expect it!
The error occurred while evaluating nil.connection= error”.i got tis
eror when i used g.messages line in my code.my code is
require ‘rubygems’
require ‘gmailer’
class FirstController < ApplicationController
def index
count=0
puts “lllllllllll”
if GMailer.connect(‘myaccountgmail.com’, ‘password’)
puts “CONNECTED”
begin
GMailer.connect(‘[email protected]’, ‘password’) do |g|
puts “GGGGGGGG enter”
puts g.inspect
g.messages(:label => “inbox”) do |ml|
ml.each_msg do |conversation|
Email.create!(
:title => conversation.subject,
:text => conversation.body,
:sender => conversation.sender,
:sender_email => conversation.sender_email,
:reply_email => (conversation.reply_email.blank?) ?
conversation.sender_email : conversation.reply_email
)
count += 1
puts conversation.archive
end
end
end #connect
rescue Exception => e
puts e
end
end
count
end