↓ Archives ↓

Send email attachments to ownCloud

Three years have passed since my article about sending email attachments to Dropbox. A lot of stuff happened since then. For instance, since the end of safe harbor, we don’t trust U.S. based cloud providers as much as we (maybe) used to.

So, here’s an update on how to automatically save mail attachments (e.g. invoices and receipts) in a specific folder in ownCloud. The basic idea is to set up a billing@example.com email address and store all received PDF files in ownCloud.

So here, we go. We’re still using Uberspace for this because we still like it – even after three years. But again, you should be able to adapt this for any Linux based setup.

Configuring qmail and reformime

At Uberspace, you get an unlimited number of email 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 emails coming this way is governed by a small file named ~/.qmail. In much the same way, you can use any email address that follows this format:

username-foo@hostname.uberspace.de

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

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

| /usr/bin/reformime -X /bin/sh -c "if [ "\${FILENAME#*.}" == "pdf" ]; then curl -X PUT -u username:password \"https://owncloud.example.com/remote.php/webdav/email-inbox/$(date +%Y%m%d%H%M%S)_\$FILENAME\" --data-binary @- ;fi"

Yep. That’s one single line. It uses reformime to extract all file attachments and then uploads those that end in .pdf to the email-inbox/ folder in your ownCloud.

Of course, it will be a good idea to create a separate user in your ownCloud which only has (create only) access to your email-inbox/ folder and use its credentials for the curl above.

Other than that, you’re all set. Now redirect your billing@example.com address to peter-owncloud@phoenix.uberspace.de and use that for services that send you invoices!

6 Comments

  • […] Dropbox much anymore since the whole NSA stuff and the end of safe harbor agreement. The above also works great with ownCloud though, […]

  • Mar 4th 201616:03
    by sthag

    Thank you for this nice little tutorial!

    I have a question. When you say “It uses reformime to extract all file attachments”, does extract mean the attachment is uploaded to the owncloud and then deleted within the email?

    If not, is it possible to achieve such a behaviour. Maybe the attachement could be replaced with a textfile with the same name and a link to the owncloud within.

  • Apr 4th 201613:04
    by Jan Schulz-Hofen

    Nope, the script does not mess with the emails. It simply *reads* the attachments and puts them into ownCloud.

  • Jun 8th 201621:06
    by Chris

    Great idea!
    I found out by accident: if the filename contains – then the file won’t be uploaded to my owncloud. And it’s always possible that filenames contain this or other special characters because the sender will most likely not be aware of this limitation or even care.

    If I wanted to upload pdfs *and* zip archives to the cloud, what would the if statement look like?

    Can I upload *all* attachments (and only those) to owncloud as well? I always get an extra-file for each email. I probably didn’t get the code right? Any idea how this works?

    Anyway, thank you very much.

  • Jul 7th 201600:07
    by Jan Schulz-Hofen

    Chris, if you want PDF and ZIP, if would be:

    if [ “\${FILENAME#*.}” == “pdf” ] || [ “\${FILENAME#*.}” == “zip” ];

    If you want all attachments, simply remove the if; then …; fi but keep everything in between.

  • Sep 25th 201615:09
    by Tom

    Hi Jan,

    very nice and powerful script. I´m desperately trying to rewrite it, so that the attachment is not uploaded to ownCloud, but in a directory in my Uberspace webroot. Trying to replace the “curl…” part with “cp…” But no success so far. Any advice for me?

    TIA

    Tom

  • Leave a Reply