0

I have setup an alias in /etc/aliases so that each time an email comes in to a specific address, the text of the email is sent to a Ruby script. Like so:

example: |/etc/smrsh/my_script.rb

I need to know how to read the piped data in my Ruby script.. I have written a simple Perl script that can read the data.. just can't figure out how to do it in Ruby.

Here is the relevant lines in the Perl script:

my $fout = "/tmp/email.out";

open( EM, ">$fout" );

while( <> )  {
    chomp;
    print EM "$_\n";
}
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497

1 Answers1

3

You can use STDIN to read your pided data. The equivalent of your Perl code would be something like:

out = File.open("/tmp/email.out", "a+")
STDIN.each do |line|
  out.puts line
end
molf
  • 73,644
  • 13
  • 135
  • 118
  • This worked great, thank you. As a side note, in order to get the alias to work properly, I had to create symlink to the ruby binary in /etc/smrsh and then define the alias as: my_alias: "|/etc/smrsh/ruby /etc/smrsh/my_script.rb" And that did it! Thanks! –  May 14 '09 at 16:08
  • help me look at this http://stackoverflow.com/questions/36072489/ruby-script-sending-received-email-as-sms?noredirect=1#comment59792534_36072489 – acacia Mar 18 '16 at 12:14