↓ Archives ↓

Archive → December, 2012

Send E-Mail attachments to Dropbox

At Planio and LAUNCH/CO, we receive a lot of PDF receipts for things we purchase online or services we use regularly. I want all those files to go directly into a designated folder in my Dropbox and I don’t want to look at them until the end of the month when I zip everything together and send it to our accountant.

For this purpose, I have been using a service called send to dropbox for a while until it started getting unreliable. Soon it didn’t upload any of the e-mails anymore without even supplying an error message and I ended up fiddling with my e-mail attachments by hand again. Not very funny.

So, I hacked together something that “works for me” (TM) and doesn’t rely on an external service. Well, in fact, I am still relying on Uberspace to host this, but (a) I could easily do this everywhere else and (b) it’s a huge improvement over send to dropbox which felt more like a blackbox.

This is how it’s done on Uberspace, but really the instructions are very much applicable to any Linux based setup.

Download and Setup Dropbox Uploader

Dropbox Uploader is an awesome little shell script written by Andrea Fabrizi which can be used to upload files to Dropbox from the command line. It is very handy since it has no dependencies except for curl. For my purpose, it needed a little tweaking since it would not accept files from standard input, so I forked it and am currently waiting if my pull request is being merged. I will update this article as soon as it does, since I have no interest in maintaining the fork. Andrea seems to be doing an awesome job with this.

So open a shell on your Uberspace and do the following:

mkdir -p ~/opt
cd ~/opt
git clone git://github.com/yeah/Dropbox-Uploader.git
mkdir ~/bin
ln -s ~/opt/Dropbox-Uploader/dropbox_uploader.sh ~/bin/

This installs the latest version of Dropbox Uploader in ~/opt/Dropbox-Uploader and creates a handy symlink to it in ~/bin which should be in your PATH already.

To verify if that last assumption is true (and to authenticate Dropbox Uploader), simply run

dropbox_uploader.sh

It should lead you through the process of creating your own Dropbox App and authenticating your copy of Dropbox Uploader against it. If asked for the Access Level I’d opt for App folder since this won’t give the app access to your entire Dropbox. Your credentials are stored in ~/.dropbox_uploader which is 600 by default, but if you’re a bit paranoid like me, App folder is still the safer bet.

If you’d like to test Dropbox Uploader, perform a simple upload like this:

echo "hello dropbox world?" | dropbox_uploader.sh upload - hello_world.txt

A file called hello_world.txt should magically appear in your Dropbox. Does it work? Yay! Then here comes the fun part…

Configuring qmail and reformime

Again, it should not be hard to adapt this to other MTAs, but since Uberspace uses qmail (or netqmail to be more specific) this tutorial is written for that.

At Uberspace, you get an unlimited number of e-mail addresses out of the box. Your primary is composed like this:

username@hostname.uberspace.de

Where username is your Uberspace username and hostname is the host your account is hosted on. What happens to e-mails coming this way is governed by a small file named ~/.qmail. In much the same way, you can use any e-mail address that follows this format:

username-foo@hostname.uberspace.de

Where foo can be anything you like. To specify what should happen with e-mails coming in via this address, you can create a file called ~/.qmail-foo.

So, for instance, if you want all e-mail PDF attachments sent to peter-dropbox@phoenix.uberspace.de to appear in your Dropbox, create a ~/.qmail-dropbox file with the following content:

| /usr/bin/reformime -X /bin/sh -c "if [ "\${FILENAME#*.}" == "pdf" ]; then ~/bin/dropbox_uploader.sh upload - \"\$FILENAME\";fi"

Yep. That’s one single line. It uses reformime to extract all file attachments and then uploads those that end in .pdf to your Dropbox using Dropbox Uploader.

Try it out. It should already work. There’s nothing else to do.

Update: We don’t trust Dropbox much anymore since the whole NSA stuff and the end of safe harbor agreement. The above also works great with ownCloud though, too.