Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created June 15, 2015 02:07
Show Gist options
  • Save komasaru/0cbfe02794efa4c1a09e to your computer and use it in GitHub Desktop.
Save komasaru/0cbfe02794efa4c1a09e to your computer and use it in GitHub Desktop.
Ruby script to get a mail via alias of postfix.
#! /usr/local/bin/ruby
# coding: utf-8
#-------------------------------------------------
# Ruby script to get a mail via alias of postfix.
#-------------------------------------------------
require 'mail'
class GetMail
def initialize
dt = Time.now.strftime("%Y%m%d_%H%M%S%L")
@out_file = "/path/to/#{dt}.txt"
end
def execute
open(@out_file, "w") do |f|
mail = Mail.new($stdin.read)
f.puts "From: #{mail.from.first}"
f.puts "To: #{mail.to.first}"
f.puts "Date: #{mail.date}"
f.puts "Subject: #{mail.subject}"
f.puts "Body:\n#{mail.body.decoded.encode("UTF-8", mail.charset)}"
end
rescue => e
$stderr.puts "[#{e.class}] #{e.message}"
e.backtrace.each{|trace| $stderr.puts "\t#{trace}"}
exit 1
end
end
exit unless $0 == __FILE__
GetMail.new.execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment