How do I painlessly receive mail in linux and feed it to Rails?
Asked Answered
S

3

8

I've been googling all night for postfix howto's, but still I couldn't figure out how do I manage to receive email on linux-server (being more specific - Ubuntu).

All I need is a catch-all mailbox which gets all emails and feeds them to the ruby script (which then passes it to Rails, of course). I was able to set-up postfix for sending emails a while ago and I have to say - it wasn't painful at all, I did almost nothing besides actually installing it.

Could you suggest a good howto, or a recipe, or an alternative easy-to-setup mail-server that could solve the issue?

Subgroup answered 24/5, 2009 at 0:17 Comment(0)
B
5

There are two parts to this answer, Norman Ramsey's answer covers the 2nd part: handing off the email to a script to be processed. The first part is configuring Postfix to receive the email. Since you need a catch-all you can put something like this in /etc/postfix/aliases

@yourdomain.com localuser

And 'localuser' is the name of the account on your system which has a

/home/localuser/.forward

which contains the command (see Norman's response). Or, you could keep it all in Postfix

/etc/postfix/aliases:

@yourdomain.com |/path/to/your/script

This will send all email that is sent to @yourdomain.com and sends it to your script for processing. Keep in mind that the script will be executed as the postfix user, so you will need to make sure your environment is setup appropriately (for instance you are not relying on a specific $PATH that your normal user account has). The postfix user likely has a very basic environment (e.g. might not even have /usr/local/bin in their $PATH)

Braxton answered 24/5, 2009 at 1:21 Comment(2)
Dont forget to run 'newaliases' (as root or via sudo) after you edit the aliases file - this will regenerate the (Berkeley?) DB file which is what Postfix uses for fast lookups.Braxton
Does this still work? It doesn't seem to be working on my Ubuntu 16.04 server.Veronique
G
6

Choose the account you wish to receive mail that is forwarded to your Ruby script. Edit the .forward file in the home directory of that script to read

"|/path/to/my/ruby/script"

When postfix delivers mail to the account, it will run the script with the permissions of the designated user and will provide the mail on standard input. Depending on what you do with the mail you may want to authenticate it in some way so that the script knows it is really from you. (E.g., header with salt and SHA1 hash of salt+password.)

Here's a real-life example from my own hairy mail system:

"|/home/nr/bin/filtermail /home/nr/machine/x86-bsd/bin/luapipe /home/nr/machine/x86-bsd/bin/safe-slocal 2>>/home/nr/slocal.log 1>&2"

This goes to a shell script which then calls both Lua and C programs to dispose of mail properly.

Gigantopithecus answered 24/5, 2009 at 0:50 Comment(2)
Your answer is very brief ) Which user should have this ".forward" file? Does it work for postfix?Subgroup
Whichever user receives the mail, and yes it works for postfix.Gigantopithecus
B
5

There are two parts to this answer, Norman Ramsey's answer covers the 2nd part: handing off the email to a script to be processed. The first part is configuring Postfix to receive the email. Since you need a catch-all you can put something like this in /etc/postfix/aliases

@yourdomain.com localuser

And 'localuser' is the name of the account on your system which has a

/home/localuser/.forward

which contains the command (see Norman's response). Or, you could keep it all in Postfix

/etc/postfix/aliases:

@yourdomain.com |/path/to/your/script

This will send all email that is sent to @yourdomain.com and sends it to your script for processing. Keep in mind that the script will be executed as the postfix user, so you will need to make sure your environment is setup appropriately (for instance you are not relying on a specific $PATH that your normal user account has). The postfix user likely has a very basic environment (e.g. might not even have /usr/local/bin in their $PATH)

Braxton answered 24/5, 2009 at 1:21 Comment(2)
Dont forget to run 'newaliases' (as root or via sudo) after you edit the aliases file - this will regenerate the (Berkeley?) DB file which is what Postfix uses for fast lookups.Braxton
Does this still work? It doesn't seem to be working on my Ubuntu 16.04 server.Veronique
W
-1

Also check out Jason Seifer's article for more details.

http://jasonseifer.com/2009/04/24/receving-email-with-rails

Weis answered 11/12, 2009 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.