A Ruby script to add email disclaimers

J

Johann Spies

A few years after my first contact with ruby (and not using it) I am
like a newbie again.

I want to use a ruby script to add a disclaimer to a message passed to
it by exim.

So far I can add a disclaimer to a message file by the following
script:
=============================
#!/usr/bin/ruby

require 'tmail'

mail = TMail::Mail.load( ARGV[0])

disklymer = "\n\nDit is 'n aangeplakte disklymer hierdie... :( \n"

if not mail.key?("X-Disclaimer") then
mail.[]=("X-Disclaimer","added")
if mail.multipart? then
mail.preamble += disklymer

else
mail.body += disklymer
end
end
puts mail.encoded
==============================

But when exim calls the script the message is not saved in a temporary
file. The script should thus be able to handle a message passed to it
like in the following command:

cat <email-message> | disclaimerscript.rb

I have experimented a little bit with "/usr/bin/ruby -n" and $_ but I
did not have success.

I would appreciate some help with this one please.

Regards
Johann
--
Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

"Therefore being justified by faith, we have peace with
God through our Lord Jesus Christ." Romans 5:1
 
R

Robert Klemme

Johann said:
A few years after my first contact with ruby (and not using it) I am
like a newbie again.

I want to use a ruby script to add a disclaimer to a message passed to
it by exim.

So far I can add a disclaimer to a message file by the following
script:
=============================
#!/usr/bin/ruby

require 'tmail'

mail = TMail::Mail.load( ARGV[0])

disklymer = "\n\nDit is 'n aangeplakte disklymer hierdie... :( \n"

if not mail.key?("X-Disclaimer") then
mail.[]=("X-Disclaimer","added")
if mail.multipart? then
mail.preamble += disklymer

else
mail.body += disklymer
end
end
puts mail.encoded
==============================

But when exim calls the script the message is not saved in a temporary
file. The script should thus be able to handle a message passed to it
like in the following command:

cat <email-message> | disclaimerscript.rb

I have experimented a little bit with "/usr/bin/ruby -n" and $_ but I
did not have success.

I would appreciate some help with this one please.

I'm not 100% sure about your use case. If your script is invoked like
this:

add-disclaimer.rb mail-file

then you can use option "-i.bak" which does in place modification and
saves the original in a backup file (or leave the ".bak" in that case you
get just the modified file).

If you want to use it in a pipe context, I'd try

mail = TMail::Mail.load( ARGF )

or

mail = TMail::Mail.load( STDIN )

Dunno which of the two TMail can handle. A quick glance at the sources
reveales that maybe you have to implement a sub class of Port to handle
reading from streams.

Kind regards

robert
 
B

Brian Schröder

A few years after my first contact with ruby (and not using it) I am
like a newbie again.
=20
I want to use a ruby script to add a disclaimer to a message passed to
it by exim.
=20
So far I can add a disclaimer to a message file by the following
script:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D
#!/usr/bin/ruby
=20
require 'tmail'
=20
mail =3D TMail::Mail.load( ARGV[0])
=20
disklymer =3D "\n\nDit is 'n aangeplakte disklymer hierdie... :( \n"
=20
if not mail.key?("X-Disclaimer") then
mail.[]=3D("X-Disclaimer","added")
if mail.multipart? then
mail.preamble +=3D disklymer
=20
else
mail.body +=3D disklymer
end
end
puts mail.encoded
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D
=20
But when exim calls the script the message is not saved in a temporary
file. The script should thus be able to handle a message passed to it
like in the following command:
=20
cat <email-message> | disclaimerscript.rb
=20
I have experimented a little bit with "/usr/bin/ruby -n" and $_ but I
did not have success.
=20
I would appreciate some help with this one please.
=20
Regards
Johann
--
Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch
=20
"Therefore being justified by faith, we have peace with
God through our Lord Jesus Christ." Romans 5:1
=20
=20

$ echo "this is a test" | ruby -e 'puts $stdin.read; puts "Disclaimer"'
this is a test
Disclaimer

regards,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
J

Johann Spies

$ echo "this is a test" | ruby -e 'puts $stdin.read; puts "Disclaimer"'
this is a test
Disclaimer

Thanks. Also thanks to Robert Klemme.

The use of $stdin.read was wat I was looking for:

mail = TMail::Mail.parse( $stdin.read )

does the job.

Regards

Johann
--
Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

"Therefore being justified by faith, we have peace with
God through our Lord Jesus Christ." Romans 5:1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top