How-To Self-Reference

Not knowing perl, I set out to write my own script in Ruby to send auto-responses from e-mail addresses setup in a vmail folder structure. You don’t need to use postfix, just have the ‘new’ folder where new messages are stored.

I recently moved our company’s e-mail server to a new VPS on slicehost and started using postfix. I followed this great tutorial on How-ToForge (except for the squirrelMail part) to setup a mysql database with the vmail folders to store the mail. I couldn’t get the Autoresponder in the tutorial to function correctly. Actually, it was ‘eating’ random mail, which was a weeks worth of headache in itself. So, we rolled out without any auto-response capabilities.

I’ve been half-heartedly looking for another solution every few weeks, but haven’t found anything other than a few Perl scripts (I’m not really famaliar with perl). So, yesterday I decided to write a ruby script to send auto-responses. I wanted to use text files to hold each autoresponse. Then, I’d just move them int and out of a folder to activate and deactivate them. I also took advantage of the ‘new’ folder within the vmail structure to reply to mail newer than the autoresponder config file.

The format of the config files is:

email_address_to_respond_from

subject of response

message of response

The message can be multiple lines until the end of the file.

For each config file in the directory, the script parses the file for the above information. Then it will check that user’s new mail folder for files. Any new mail file which was modified (sent) after the modification date of the config file is parsed to get the sender’s address(es)*. A new e-mail is composed using benprew’s pony gem and sent to each of the senders. Finally, the script ‘touches’ the config file to move the modification date newer than the messages for which it just responded.

The nifty parts of the script are iterating over a directory listing of files:

Dir.new(directory_path).each { |filename| puts filename }
and using the stat method of a file object to get the modified dateTime:
file.stat.mtime
and lastly, using the FileUtils module to ‘touch’ a file:
FileUtils.touch “#{file_path}/#{file_name}”

Here’s the full source:

– Chris

Image

Christopher R Marshall

@codegoalie

Enjoys programming web applications; especially in Ruby and Go. Also enjoys playing ice hockey as a goalie and playing the guitar.

Categories