As I posted earlier in my introduction to the new blog, I said that one of the main things I liked about Posterous was being able to create a blog post from an email. It serves two pretty cool purposes for me.
First, it allows me to write a blog post anywhere and send it at will. While I’m not the biggest blogger ever, it was definitely a convenience factor for me. However, with the Dropbox integration, this becomes less of an issue, because I can write a post pretty much anywhere now, including my iPad.
But, more importantly, it gave me a way to send content from other services and import directly into my blog. A blog for me is kind of a collection of rag tag stuff… photos from Flickr and Instagram, articles that I star in Google Reader, etc. Now that I don’t have a common API like I did with Posterous, it becomes a challenge to get content into my blog automatically. I’ll go more into how I got around that later on.
Getting Email In
Since I have a dedicated Ubuntu server for several sites of mine on Linode, I have the power of being able to hack up Postfix to bend to my wills. This probalby isn’t the case with everyone, but if it does apply, please read along.
The idea was to create a hidden email address on my main domain, take emails from that and get them to a ruby script.
The first thing I did was create a quick and dirty script that takes an incoming email
over stdin and processes it into the correct directory (either _posts or
_drafts, depending on tags – more a in sec). The way I tested this was
just to take a full email that I sent to Posterous earlier, grab the
source of it (Show Original in Gmail), and put that into a text file.
Then just pipe that into my script (cat email.txt | receive-email.rb)
and I was good to go. Sure I could’ve created a test suite, but like I
said, it was quick and dirty.
Some things of note, here. I’ve taken the Posterous system of being able
to put metadata tags right into the subject line. So, it uses the
majority of the subject as the post’s title, and filters out any tags
that look like: ((key: value)). Right now, it handles tags ((tags:
tag1, tag2)), the ability to use drafts ((draft: true)), type of markup:
((markup: html)), slug ((slug: this-is-the-slug)) and layout ((layout:
post)).
Files are written out to the appropriate dropbox folder, and picked up automatically to rebuild the site. I’ll describe that in detail in another post.
Postfix config
It took a bit of playing to get this working in Postfix, but I did eventually get it. Without going into crazy detail on stuff that’s already been rehashed several times on other posts, let’s just say that putting a script into your alias file is a bad idea because the script will be run as root. It’s better to use the transport method to pipe emails to a particular script – sort of how something like Spamassassin would be setup.
In your master.cf, you would add another handler to the bottom similar to this:
1
| |
It very simply takes any emails with the “jekyll” transport and pushes them to the script that I setup. Notice it’s an .sh and not the .rb. This was because I have rvm setup globally, and it’s a bit difficult to get the environment params correct. Basically, it’s just setting the environment and pushing all of stdin to my script – no big deal.
Because I’m hosting my tedkulp.com domain on this server as well, I can’t just push all of my email to jekyll. So, for my hidden email address, I use a trick in my virtual file to rewrite it to an unusued subdomain, which in my case is hidden.tedkulp.com. My line in the virtual file looks something like:
1
| |
This is only rewritten for the purposes of Postfix, and doesn’t translate to anything else. It’s only purpose is to allow me to write a custom transport rule for hidden.tedkulp.com.
The last trick now is to setup my transport file to take all emails to hidden.tedkulp.com and push them to jekyll. Since it’s not a real subdomain, it’s only going to see emails from my rewritten hidden email address. It looks something like:
1
| |
A quick postfix reload, and you’re good to go.
If then what now?
Now for the issue of the automated posting. Since we have our email address to send content to, we just need something to do it for us.
There’s a new service that’s still in early beta that is making that process a WHOLE lot easier. IfThisThenThat (lovingly called ifttt) is a service that allows you to take triggers from one thing and apply them to another thing. So, basically, you can make a task that will, say, email your hidden email blog address everytime you create a new photo in Instagram. It’s stupidly powerful, totally nerdy, and downright awesome. They’re really responsive on twitter and very receptive to new ideas, which is great. I’ve been using it for a few months now, but it really didn’t becomes THAT useful until the blog switch.

Here is an example of the template for the email that’s sent from my Instagram task. Simple as can be…

Now, I have all of my automated posts emailed to me to my secret email address. Awesome!
In my next post, I’ll discuss how to get my posts from Dropbox to automatically show up in Jekyll without touching the sever.




