#!/usr/bin/env ruby require 'mail' # This script is a dead-simple Mail Delivery Agent (MDA) that exports the parts of the received email as # environment variables and calls a shell script that can make use of these variables to further process # the mail. mail = Mail.new(ARGF.read) # Expose mail properties as environment variables %w[date message_id from to subject body].each{|field| value = mail.send(field.to_sym) value = value.join(',') if value.respond_to?(:join) ENV["TWMAIL_#{field.upcase}"] = Shellwords.escape(value.to_s) } # The hook to be executed is read from the environment variable TWMAIL_HOOK # If none is set, this script will assume that twmail_on_new_mail is in the # $PATH and executable. cmd = ENV["TWMAIL_HOOK"] || 'twmail_hook' # Call hook script %x[#{cmd}]