« MT Plugin to Attach Bookmark Counts on Delicious | Main | NHK started broadcasting their radio program on business English online »

Twittering via E-mail

There must be many people who'd like to post their status to Twitter from cell-phones. In my case, Twitter hasn't supported the cell-phones provided by Japanese careers yet, or to be exact, Japanese cell-phones don't support International SMS, so I've been forced inconvenience on not to be able to do it. However, it's nonsence that I only wait for them to concider my circumstance. I have to help me for myself.

Today, I found DreamHost, which hosts this blog and other websites of mine, supported piping E-mails into other programs. Great! I went straight to write a tiny script, mail2twitter.rb, which allows me to post my status via e-mail. Then I created a new user to accept the e-mails. Finally, created a file named .forward.postfix right under the user's home directory like this:

"| /home/username/path/to/mail2twitter.rb"

That's all. For more, consult DreamHost Wiki to know how to configure your environment. And I drew upon a script written by ma.la to write this script. Thanks!

Note:

  • Don't forget to give the script an executable privilege(e.g. chmod +x mail2twitter.rb).
  • Hide the address to accept e-mails from anyone. mail2twitter.rb checks who sends an e-mail by From field of it, but, you know, it's inadequate to save your status from malicious people.
  • This way explained here is basically applicable to other environments which support e-mail piping.

mail2twitter.rb

#!/usr/bin/ruby

require 'uri'
require 'kconv'
require 'mailread'
require 'net/http'

username  = 'username'
password  = 'password'
addresses = ['foo@exapmle.com', 'bar@example.com']

mail      = Mail.new($stdin)
exit unless addresses.find{|address| mail['From'].index address}

status    = URI.escape(Kconv::kconv(mail['Subject'], Kconv::UTF8, Kconv::AUTO))
req       = Net::HTTP::Post.new('/statuses/update.json')
req.body  = 'status=' + status
req.basic_auth username, password

Net::HTTP.new('twitter.com', 80).start {|http|
  http.request req
}

Comments (2)

That's a great idea! I didn't know Dreamhost supported that. I use Dreamhost for my blogs as well.

どうもどうも!

Thank you for posting a comment! And DreamHost rocks!

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on April 15, 2007 12:45 AM.

The previous post in this blog was MT Plugin to Attach Bookmark Counts on Delicious.

The next post in this blog is NHK started broadcasting their radio program on business English online.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.34