Question

We are running the Postfix mail transit program. We've specified that when an incoming email comes in, to send it to a Ruby script.

The commands in postfix are:

flags=Xhq user=vmail argv=/usr/local/rvm/rubies/ruby-2.1.1/bin/ruby /home/usera/rails/project/lib/email_receiver_script

When we receive an email, the mail.log has an error in it saying:

Apr  6 20:02:48 railsdev postfix/pipe[25961]: B447A282D01: to=<rr@myapp.com>, relay=send_to_ruby_filter, delay=0.06, delays=0.01/0/0/0.05, dsn=5.3.0, status=bounced (Command died with status 1: "/usr/local/rvm/rubies/ruby-2.1.1/bin/ruby". Command output: /usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mail (LoadError)  from /usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'  from /home/usera/rails/project/lib/email_receiver_script:4:in `<main>' )

However the command runs just fine using the command line. If I take email.txt and pipe it to the same command the mail gets delivered to the script properly. Like so:

cat email.txt | /usr/local/rvm/rubies/ruby-2.1.1/bin/ruby /home/usera /rails/project/lib/email_receiver_script

The first few lines of our script are:

#!/usr/bin/env ruby 
require 'rubygems'
require 'mail'
require 'redis'
require 'resque' 

RVM was used as root to install Ruby globally.

It seems like the postfix user 'vmail' cannot find the Rubygems installed. Any ideas how we can make it execute the script properly?

Was it helpful?

Solution

After going to the end of the internet and back again I have found a solution. Use RVM wrappers. Once the wrapper is made then you need to bundle the gems into the wrapper using rvm 2.1.1@global do bundle install. After that I changed the postfix command to:

flags=Xhq user=vmail argv=/usr/local/rvm/wrappers/ruby-2.1.1@my_app/ruby /home/usera/rails/project/lib/email_receiver_script

There is additional information at "Getting Postfix to Send Incoming Mail to Rails".

OTHER TIPS

Since you are passing the ruby path like:

cat email.txt | **/usr/local/rvm/rubies/ruby-2.1.1/bin/ruby**
/home/usera /rails/project/lib/email_receiver_script

Try not using the shebang in the script? I am not sure the "/usr/bin/env ruby" returns the correct env scope.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top