Created
February 15, 2011 17:50
-
-
Save codegoalie/827900 to your computer and use it in GitHub Desktop.
Ruby script to send auto-responses see http://chrismar035.com/2011/02/15/ruby-auto-responder-for-postfix-vmail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
require 'rubygems' | |
require 'pony' | |
# Set absolute path for config file directory | |
conf_path = "/home/auto-responses" | |
#for each config file (autoresponder) | |
Dir.new(conf_path).each do |response_file| | |
# don't act on this file or linux '.' listings | |
unless response_file[0,1] == "." || response_file == "auto-response-script.rb" | |
#create a file for reading | |
file = File.new("#{conf_path}/#{response_file}") | |
# get the last modified time of the current config file | |
last_date = file.stat.mtime | |
# e-mail address to respond from is first line | |
address = file.gets.chomp | |
#skip blank lines | |
begin | |
line = file.gets | |
end while line == "\n" | |
# subject is next | |
base_subject = "#{line.chomp} (Auto-Response)" | |
#skip blank lines | |
begin | |
line = file.gets | |
end while line == "\n" | |
# take all the rest of the lines as the message | |
message = line | |
while line = file.gets | |
message << line | |
end | |
# write parsed info to output (for log file) | |
puts address | |
#puts last_date | |
#puts subject | |
#puts message | |
# split the e-mail address into user and domain to put into vmail path | |
user = address.split('@')[0] | |
domain = address.split('@')[1] | |
#puts user | |
#puts domain | |
# build the vmail path to the new mail message files | |
mail_path = "/home/vmail/#{domain}/#{user}/new" | |
#for each new mail file | |
Dir.new(mail_path).each do |mail_file| | |
#don't act on the linux '.' listings | |
unless mail_file[0,1] == "." | |
# get mail file for reading | |
mail = File.new(mail_path + '/' + mail_file) | |
#log mail filename and mod times (for debugging) | |
puts mail_file | |
puts "mail_file_time: #{mail.stat.mtime}" | |
puts "conf_file_time: #{last_date}" | |
# act only if mail file is newer | |
if mail.stat.mtime > last_date | |
# initialize flags for From and Subject lines in message | |
found_from = false | |
found_subject = false | |
#get first line from email message | |
line = mail.gets | |
begin | |
# regex to check if line starts with 'From:' | |
# if so, store as from | |
if line =~ /^From:/i && !found_from | |
found_from = true | |
from = line | |
end | |
# regex to check if line starts with 'Subject:' | |
# if so, append the base_subject from above and store as subject | |
if line =~ /^Subject:/i && !found_subject | |
found_subject = true | |
subject = "Re: #{line.chomp} - #{base_subject}" | |
end | |
#get next line | |
line = mail.gets | |
# loop over lines until both from AND subject are foud OR there are no more lines | |
end while (!found_from && !found_subject) || line | |
# use the base subject if couldn't find message subject | |
subject = base_subject unless found_subject | |
#puts "From line: #{from}" | |
# take the From line and split on spaces | |
# each 'word' which contains an @ (and isn't mailer-deamon) store into the to array | |
to = Array.new | |
from.split(' ').each do |name| | |
if(name =~ /@/ && !(name =~ /MAILER.DAEMON/i)) | |
to << name | |
end | |
end | |
# create comma separated list of email addresses; stripping angle brackets | |
to = to.join(',').gsub('<','').gsub('>','') | |
# log to whom sending mail | |
puts "Sending to: #{to}" | |
# don't send mail to nobody | |
unless to == "" || to == '[email protected]' | |
Pony.mail(:to => to, :from => address, :subject => subject, | |
:body => message ) | |
else | |
puts "To is invalid" | |
end | |
else | |
# log that the mail file is not newer than the conf file | |
puts "From: #{mail_file} is not newer" | |
end | |
# separate the mail messages in the log | |
puts "------------\n\n" | |
end | |
end | |
# update the auto response file's mod time | |
FileUtils.touch "#{conf_path}/#{response_file}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment