2012/12/10

Import mails on gmail into PostgreSQL server

I have received many emails from systems. Some are urgent alerts and some are just for my information. I wonder how does the trend of these alerts go so far. It is difficult to analyze the trend just on my mailbox so I  thought that it would be useful to import those mails into a local PostgreSQL server.

First, I enabled POP3 service on my gmail account, then I wrote some ruby code.
With some gems and ruby, I could do these task in handy.
-------------------------------------------------
require 'mail' #gem mail
require 'pg'   #gem pg

Mail.defaults do
  retriever_method :pop3, { 
    :address => "pop.gmail.com",
    :port => 995,
    :user_name => 'username',
    :password => 'password',
    :enable_ssl => true
  }
end

# connect PostgreSQL server
db = PG.connect( :dbname => 'db_name', :user => 'db_user')

Mail.all.each do |email|
  $stderr.puts email.date.to_s # Output date of email
  begin
    # Japanese character sets are often undetected.
    # So I use Content-Type of email.
    if email.content_type =~ /ISO-2022-JP/i then
      body_str = email.body.to_s.encode("UTF-8", "ISO-2022-JP")
    else
      body_str = email.body.encoded
    end
    # insert into PostgreSQL server
    db.exec "INSERT INTO mailbox ( dt, subject, body ) VALUES ($1,$2,$3);", [email.date.to_s, email.subject, body_str]
  rescue => ignore
    $stderr.puts "[error]:" + ignore.to_s
  end
end
-------------------------------------------------

And this code needs two gems, pg and mail.
Those are installed in following commands.
-------------------------------------------------
(For Windows)
gem install --no-ri --no-rdoc pg -- --with-pg-config="c:\Program Files\PostgreSQL\9.2\bin\pg_config.exe"
gem install --no-ri --no-rdoc mail
-------------------------------------------------
(For Linux)
sudo gem install pg -- --with-pg-config=/usr/pgsql-9.2/bin/pg_config
sudo gem install mail
-------------------------------------------------

Now I have been importing mails. When it is finished, it is time to analyze. That is another story.

2012/12/06

Browser test with Selenium and WebDriver

I recently try to make browser tests with Selenium and WebDriver. I found it is very useful and fan. I use to use Selenium-RC and PHP bindings and Ruby bindings. In my project, I use Selenium2, WebDriver and Java bindings.
It is easy to install with maven(pom.xml).

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.25.0</version>
    </dependency>
  </dependencies>
And I downloaded Selenium Server at Selenium site.
If you want to use Chrome for your test, Chromedriver is also needed. It could download from this site. Running Selenium Server with Cromedriver is a following command options:

java -jar selenium-server-standalone-2.25.0.jar -Dwebdriver.chrome.driver=C:\path\to\chromedriver.exe

Selenium server is now ready for accept the command from test cases. If you have enough network allowances, The server can accept from even other hosts so that you can make CI server and distributed testing clients.

2012/12/05

FedEx scam email

I received email which suggests I have an undelivered package at a postal office.

I wondered why I receive this notice because I haven't bought any items recently from oversea and FedEx is not a major delivery service in Japan domestically. Then, I found FedEx alert message on the website. It is one of the infamous scam email. The email which I received did not attached any malicious files, but I need to be careful about this kind of emails.