Friday, April 17, 2009

Gmail Automation with Ruby and Mechanize

Found a post online and figured I'd try to do this myself. Works surprisingly well when each line is pasted into irb or when using rails script/runner,
but it appears as though google is somehow blocking straight up 'ruby gmail.rb' calls.


gmail.rb:
    1 require 'rubygems'
2 require 'mechanize'
3
4 agent = WWW::Mechanize.new
5 agent.user_agent_alias = 'Linux Mozilla'
6 page = agent.get 'http://www.gmail.com'
7
8
9
10 # #log into gmail
11 form = page.forms.first
12 form.Email = 'user.name'
13 form.Passwd = 'password'
14 page = agent.submit(form, form.buttons.first)
15
16
17 # switch to basic view
18 page = agent.get page.search("//meta").first.attributes['href'].to_s.gsub(/'/,'')
19 page = agent.get page.uri.to_s.sub(/\?.*$/, "?ui=html&zy=n")
20
21
22 #create and send the email.
23 page = agent.click page.links.find { |l| l.text =~ /compose/i }
24
25 form = page.forms[1]
26 form.to = 'someone.else@gmail.com'
27
28 form.subject = 'test subject'
29 sleep 3
30 form.body = <<EOF
31 "Hi,
32
33 I would like to spam you using ruby mechanize and gmail.
34
35 "
36 EOF

37 page = agent.submit(form, form.buttons.first)
38
39

No comments:

Post a Comment