Sep 14
2007

Quick and dirty Ruby CGI script to collect email addresses

I needed to quickly throw together a CGI script to grab email addresses from a form. If you couldn’t tell, Ruby is my language of choice.

It’s kind of weird not doing some full-blown Rails application, so I actually had to go and look up how to configure Apache to run CGI scripts again.

It expects you to have Ruby, FasterCSV and Ruby Gems installed on your machine.

All it does is collect email addresses to write to an Excel-compatible CSV file, then renders a thank you page (sign_up_thanks.html, you should create one).

#!/usr/bin/env ruby

# This simple CGI ruby script is to collect email
# addresses of fans that might want to sign up for our mailing list.
#
# It's just a CSV file, with a format like so:
# name, email_address, sign_up_date
#

require 'rubygems'
require 'cgi'
require 'fastercsv'

DBFILE = 'emails.csv'
OUTPUT = 'sign_up_thanks.html'

# Parse CGI params
cgi = CGI.new

# Append to CSV file
FasterCSV.open(DBFILE, 'a') do |csv|
  csv << [cgi['name'], cgi['email'], Date.today]
end

# Render thank you page...
cgi.out('text/html') { File.read(OUTPUT) }
Written by Seth B

As Principal of Subimage LLC, Seth spends most of his days improving Cashboard. Occasionally he finds time to write about music, design, startups, and technology.

Tagged: ruby