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

醉酒当歌 提交于 2019-11-30 05:25:56

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)

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!