randomwisdom.com

July 19, 2005

Keitai-mail

Filed under: — Jeff @ 5:42 pm

I've gotten a couple inquiries lately as to exactly how I posted photos and such from my phone while I was in Japan. Quite a few people wanted to use the code for their own blogs, so here it is. I'm gonna try to turn this into something moderately professional, but I'm gonna need lots of input and bug reports, and probably a whole code rewrite when I finally get around to it.

Current version: 0.4
Tar.gz archive (UNIX)

Features (and why you should use this script)

While I've seen a lot of great blog posting plugins, all of them have one thing in common: they require the mail processing stuff to reside on the same server as your blog. In many cases, this just isn't feasible. What if you can't run perl scripts on your webhost? What if your webhost doesn't accept mail, or the mail is delivered to a different server? What if your webhost won't install procmail or the modules you need? What if you want your posts to go up immediately instead of on a batch timer? Keitai-mail solves these problems by processing the mail on whatever server you install it on, and contacting your blog remotely. If your blog supports the metaWeblog interface through XML-RPC (it probably does) then you can use Keitai-mail.

Keitai-mail automagically translates the encoding of your mail from the default encodings of most programs into the standard UTF-8 encoding used by most modern weblog software. Want to post from your Japanese keitai in Japanese? From your Russian phone? No problem.

It also takes image attachments from your email, comes up with a unique name for each one, uploads them to your webhost, and links them into your post.

Installation

You will need these perl modules: XMLRPC::Lite and MIME::Parser (and their dependencies). If you have shell access you can use cpan to install them, or you can pester your system admin until he or she does it for you.

The script has one caveat: the script has to run on the same machine that processes the mail. However, this does not have to be the same machine that hosts your blog! In fact, if you have access to a server that processes mail for, say, cs.arizona.edu you could send mail to joesmith@cs.arizona.edu and have it post to a blog on randomwisdom.com or plaristocrates.com.

You'll also need to do several things to get it set up.

1. Set up email forwarding through procmail. Sometimes this step isn't necessary. Assuming procmail is set up, you can create a file called ".forward" in your user directory that contains a line like this (include the quotes):
"|IFS=' ' && p=/usr/local/bin/procmail && test -f $p && exec $p -Yf- || exit 75 #username"
This exact line probably won't work. Ask your administrator what's best, especially considering that if this line doesn't work it can seriously delay any mail sent to you. If you don't know your administrator yet, now's a good time to make friends.

2. Set up procmail to redirect certain mail through the script. Email forwarding with procmail works with something called recipes, which reside in a file probably called ".procmailrc" (which won't exist yet). The important 4 lines (change /home/me to whatever your home directory is):

LOGFILE=/home/me/procmail.log
:0c:/home/me/blogpost.lock
* ^From:.*yourmail@host.com
|/usr/bin/perl /home/me/blogpost.pl -c/home/me/.blogpostrc >> /home/me/blogpost.log

The first line makes a logfile so that we can make sure procmail is actually getting the email in the first place. The second line makes sure a COPY of the mail is forwarded to the script, and only one at a time (so we don't get competing threads in case a mail is duplicated). The third line says what conditions it's forwarded under. (For me it was sufficient to perform a VERY general check on the email's From: line... security by obscurity. You can add header checks or anything else that identifies the mail as coming from your phone; look up a tutorial on regex if the syntax confuses you. It's the same as UNIX "grep".) The last line tells what command to execute; in this case you're "piping" the mail to perl and its script, with the config file flag, and appending output to a logfile. You could probably simplify this quite a bit. Play around with it until you get it working.

3. Copy the script to that directory, with the appropriate permissions (755 should work). Make sure you read through the script! A couple items in there were specific to my configuration, but they should all be documented at the beginning of the file. It probably won't work right out of the box without a teeny bit of tweakage, especially since it's still pretty much in beta. Let me know if something isn't quite clear.

4. Make your ".blogpostrc" file. The directions in the script should be pretty self-explanatory, but I've included an example config file to help make things clear. Categories should be the same exact string as the category you want to add the post to-- and yes, capitalization matters! I strongly recommend setting up a separate user account with minimum privileges for your Keitai-mail posts, since the config file's password is stored in plain text.

5. Try it out by sending an email to that address from the arranged address. If you're really lucky, it might just work on the first try! But since we don't live in an ideal world, something will probably get stuck along the way.

Things that can go wrong

  1. Procmail isn't getting the mail at all.
    In some cases the server that processes the mail isn't the one you have access to, in which case the procmail on your system will never see the email at all. This conundrum is something you'll have to work out with, you guessed it, the system administrator.
  2. Procmail gets the mail, but sends the wrong stuff to blogpost.pl, or doesn't send anything to blogpost.pl
    If you've set up procmail to log what it's doing, you should get a nice procmail.log file in your home directory. You can add the following line to the beginning of your .procmailrc file in order to see exactly what procmail sees:
    VERBOSE=on
    I'm no expert on procmail, and if the problem isn't immediately apparent, it's probably in your best interest to consult a procmail tutorial.
  3. Blogpost gets the mail, but the post never shows up.
    You'll know the mail has gotten this far if you have a blogpost.log file. Read through this log and it will probably give you a hint as to what the problem is. Make sure the username, password, and blogid you specified in the .blogpostrc file are correct. Make sure the category number is valid. Unfortunately XML-RPC isn't always very descriptive if something went wrong on the blogserver's end. Keitai-mail will dump as much info as it can. If you have shell access, you can always try executing the script directly, with a valid MIME email on stdin.

Changelog

  • Version 0.4
    Cleaned up some code so that it doesn't do battle with WordPress. Did some more thorough testing.
  • Version 0.31b
    Fixed categories to better fit the metaWeblog spec. Use the names of categories now instead of numbers.
  • Version 0.3b
    Initial public release

Future work

All the libraries are there to allow a simple reply to the sender, indicating that a post was successful (or not) and which images/files, if any, were processed. A little peace of mind that your post actually went through might be handy. It's something I might include in future versions.

Configuration is a bit of a pain, especially when trying to figure out which category is which; this is something that might be easily fixed with a small test program to try making a small sample post.

Keitai-mail could easily support more file types; I just need to know what people use and what they think the best way to implement it is. For instance, should mp3 files be uploaded and then a link added to the end?

Please post your bugs and suggestions in the comments box below. For bugs, please let me know what blogging software you're using, the version, and any error messages you've received.

Leave a Reply


Powered by WordPress