I am trying to use ruby mechanize to access specific emails from my outlook web access account.
I am using the following code.
require 'mechanize'
require 'logger'
a = Mechanize.new
a.cookie_jar(HTTP::Cookies.new)
a.log = Logger.new('log1.log')
a.get('htts://webmail.xxxxxxx.org/') do |page|
my_page = page.form_with(:action => '/owa/auth.owa') do |f|
f.username = "------------"
f.password = "------------"
end.click_button
#a.cookie_jar.load('cookies.yml')
a.get('https://webmail.xxxxxxx.org/owa/Inbox/?Cmd=contents&Page=1') do |p|
file = File.new("new.xml","w+")
file.puts p.parser.to_xml
file.close
end
end
Why is this code not working?
This is working OWA retrieval script using nokogiri and mechanize to a SSL owa exchange website.
It requires rubygems, mechanize (+deps), and highline to be installed.
require 'rubygems'
require 'mechanize'
require 'logger'
require 'highline/import'
@url = 'https://email.***.***/Exchange'
@mechanize = Mechanize.new { |a| a.log = Logger.new('./log1.log') }
#In case you dont have trusted certs
@mechanize.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@mechanize.user_agent_alias = 'Windows Mozilla'
@mechanize.keep_alive = 'enable'
username = ask("Enter your username: ")
domain = ask("Enter your domain: ")
password = ask("Enter your password: ") {|q| q.echo = "*" }
@mechanize.get(@url) do |page|
form = page.forms[0]
form["username"] = domain + '\\' + username
form["password"] = password
page = form.submit
end
## Common Mailbox Schemes
@mailbox = "#{username}"
# @mailbox = "#{username}@#{domain}.#{tld}"
# @mailbox = "#{username}@#{domain}"
@inbox = @url + "/#{@mailbox}/Inbox/?Cmd=contents&Page=1&View=Unread%20Messages"
inboxlisting = @mechanize.get(@inbox) do |page|
fragment = Nokogiri::HTML(page.body)
["//img[@src='/exchweb/img/icon-msg-unread.gif']"].each do |xpathq|
puts "Found #{fragment.xpath(xpathq).count} new emails."
end
["//img[@src='/exchweb/img/icon-mtgreq.gif']"].each do |xpathq|
puts "Found #{fragment.xpath(xpathq).count} new meeting requests."
end
end
Example Script Output:
$ ruby ./owa.rb
Enter your username: john.doe
Enter your domain: mywork
Enter your password: ************
Found 31 new emails.
Found 3 new meeting requests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With