[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] How to process MAILTO forms with MacPerl



I wanted to post this information as most of the subscribers
on this list seem to be using Macperl for web related work.


Charles


Building a Visitors Book Form, submitting the results by
email, and processing the results with MacPerl.
========================================================

The problem:

Gather useful information from people visiting
my web site as well as demographic information and useful
suggestions.

-----------------------------------------------------------------

Solution:

A form with a "MAILTO" Action. I don't have access
to server scripts, so I chose submitting data by email.
The data returned is in a format suitable for computers and
difficult to read by mere mortals.

-----------------------------------------------------------------

The Form:

http://www.ozemail.com.au/~caveman/Creativity/guest_book.htm


<HTML>

<HEAD>
<TITLE>Creativity Web Site Visitors Form
</TITLE>
</HEAD>

<BODY>

<FORM METHOD="POST" ACTION="mailto:charles@jolt.mpx.com.au">

<H2>Creativity Web Site Visitors Form</H2>

Your name <BR>
<INPUT TYPE="text" NAME="FullName" SIZE="40" MAXLENGTH="40">

Age <INPUT TYPE="number" NAME="Age"  SIZE="3">

Sex <SELECT NAME="Sex">
     <OPTION SELECTED>Male<OPTION>Female
     </SELECT>  <P>

Email Address<INPUT TYPE="text" NAME="EmailAdd" SIZE="30" MAXLENGTH="30"><P>

Country <INPUT TYPE="text" NAME="country" SIZE="30" MAXLENGTH="30">

Type of Computer <SELECT NAME="Computer"><OPTION SELECTED>Windows
    <OPTION>Macintosh<OPTION>Unix
    <OPTION>NeXt<OPTION>Something else</SELECT>  <P>

What areas of creativity are you interested in?
  <TEXTAREA NAME="areaint" ROWS=6 COLS=60></TEXTAREA>  <P>

What did you like <STRONG>most</STRONG> about these pages?
<TEXTAREA NAME="likemost" ROWS=6 COLS=60> </TEXTAREA> <P>

What did you like <STRONG>least</STRONG> about these pages?
<TEXTAREA NAME="likeleast" ROWS=6 COLS=60> </TEXTAREA>  <P>

What else would you like to see at this site ..
     <STRONG>Suggestions</STRONG> please!
<TEXTAREA NAME="suggest" ROWS=6 COLS=60> </TEXTAREA>  <P>

How did you find out about the Creative Web Pages? Describe
the Web site you came from, word of mouth, magazine, etc, etc
<TEXTAREA NAME="howheard" ROWS=4 COLS=60> </TEXTAREA>   <P>

Your response will be sent be email and acknowledged
by the maintainer of the site
<P>
  <INPUT TYPE="submit" NAME="submit-a" VALUE="Send">
  <INPUT TYPE="reset" VALUE="Clear">
</FORM>
</BODY>
</HTML>

-----------------------------------------------------------------

What comes into your mail box.

I have found that responses are received as a self contained
mail message as well as attachments. The internals of the message
are the same, it must be the function of the mailer as to how
it sends the data.

Here is a sample message: (I changed some of the personal stuff!)

FullName=Jim+Smith&Age=42&Sex=Male&EmailAdd=smithj@Cloudnet.com&
country=USA&Computer=Windows&
areaint=Invention%2C+design%2C+and+implementation+of+mechanisms+which+
%0D%0Afoster+harmonious+interactions+of+humans+with+the+bioshphere.&
likemost=+Can%27t+say%2C+I+just+dropped+in.&
likeleast=+I+don%27t+like+it+that+you+are+asking+me+these+questions+
before%0D%0AI+have+had+a+chance+to+explore.++Should+I+sign+the+
guest+log%0D%0Aafter+visiting%3F++Then+put+it+at+the+exit%2C+not+
the+entrance.&suggest=+I+would+like+to+see+images+of+the+cafe+
from+the+movie%0D%0A%22Casablanca%22.&howheard=+From+Leif+Smith%27s+
%28Office+for+open+network%29+home+page.++%0D%0AAlthough+he+does+not+
have%0D%0Athe+current+address+in+his+web+site%2C+it+forwarded+
me+here%0D%0Awell+enough.&submit-a=Send


-----------------------------------------------------------------

Next problem. Decoding all those plus signs %nn and & signs.

As you can see, the fields on the form are in the form of
fieldname1=response1&fieldname2=response2....
on on line. The above sample has been split for your email
reading. Perl is perfect for such processing!


-----------------------------------------------------------------

Goal: Store the responses in a Filemaker Pro database.

My immediate method was to create a tab delimited file for
importing into FMPro, but the longer term solution is Applescript.

Here is my perl program. At present, it doesn't delete the files
it processes, leaving that to me. I'll add the unlink calls
when I'm happy with the operation.

By the way, when I get a response in the email message, I save
it is a folder (Eudora) called Forms inside a folder called Creativity.


-----------------------------------------------------------------

# creforms.pl
#
# Forms submitted from my page come into my mail box
# with the responses either embedded in the message or
# as an attachment
#
# Attachments are stored in the folder
#   MacintoshHD:System Folder:Eudora Folder:Attachments Folder
#    and have a name beginning with "Form posted"
# Embedded responses are processed by moving them into
#   a folder called MacintoshHD:System Folder:Eudora Folder:Creativity:Forms
#
# The information is self contained within one line of data
# consisting of FieldName=Value&FieldName=Value ....
# BE WARNED! Some browsers return the fields in reverse, so don't
# assume a fixed layout
#
# The only difference in how the files are processed is in the
# getting of the date submitted. The embedded messages are fine,
# because the Date can be derived from the mail header.
# The attachments will have to get the date created field from
# the file itself.

# Sample Date line from an email header
# Date: Tue, 12 Dec 95 19:35:03 -0800

# Sample line of data broken up at the & character
# FullName=Jim+Smith&
# Age=36&
# Sex=Male&
# EmailAdd=jsmith@earthlink.net&
# country=USA&
# Computer=Windows&
# areaint=I+never+thought+about&
# likemost=+&
# likeleast=+&
# suggest=+Just+nosing+about.+Hi%2C+Charles%21+--Jim+&
# submit-a=Send


require "GUSI.ph" ;
require "ctime.pl";

%monthnum = (
  "Jan", "1",
  "Feb", "2",
  "Mar", "3",
  "Apr", "4",
  "May", "5",
  "Jun", "6",
  "Jul", "7",
  "Aug", "8",
  "Sep", "9",
  "Oct", "10",
  "Nov", "11",
  "Dec", "12",
);


@fieldnames = ('EmailAdd','Age','FullName','Sex','country','Computer',
      'areaint','likemost','likeleast','suggest', 'howheard');

$filechoice = &MacPerl'Choose(&GUSI'AF_FILE, 0, "", "",
            &GUSI'CHOOSE_NEW + &GUSI'CHOOSE_DEFAULT, "feedbacks.xls");
open(RESULT, ">".$filechoice) || die "cant create file $filechoice\n";

print "Processing the attachments folder\n";
chdir("MacintoshHD:System Folder:Eudora Folder:Attachments Folder") ||
 die "couldnt go to attachments folder\n";
 $ta = 0;
 foreach $file (<Form*>) {
    print "$file\n";
    ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
      $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
    $dctime = &ctime($ctime);
    @df = split(/\s+/,$dctime);
    open(INFILE,$file) || die "couldnt open $file\n";
    while(<INFILE>) {
       chop;
       if (/FullName/) {
         &feedback(@df[2], @df[1], @df[4], $_)
       }
    }
    close(INFILE);
    $ta += 1;
 }
print "$ta attachments processed\n";


print "Processing the Forms file\n";
open (FORMS, "MacintoshHD:System Folder:Eudora Folder:Creativity:Forms") ||
    die "couldn't open the Forms file\n";
$tf = 0;
while (<FORMS>) {
  if (/^Date:/)  { ($a, $b, $day, $monthname, $year) = split(/ /); }
  if (/FullName/) {
       &feedback($day, $monthname, $year, $_);
       $tf += 1;
  }
}
close(FORMS);
print "$tf entries in Email folder processed\n";
close(RESULT);

sub feedback {
 ($day, $month, $year, $chunk) = @_;
$m = $monthnum{"$month"};

# Here is the interesting bit...I had to to guess what character
# was what. Does someone have a full list?
#
$chunk =~ s/%3A/\:/g;
$chunk =~ s/%2F/\//g;
$chunk =~ s/%28/\(/g;
$chunk =~ s/%29/\)/g;
$chunk =~ s/%2C/,/g;
$chunk =~ s/\+/ /g;
$chunk =~ s/%0D//g;
$chunk =~ s/%0A//g;
$chunk =~ s/%09/!/g;
$chunk =~ s/%27/'/g;
$chunk =~ s/%22/"/g;
$chunk =~ s/%25/\%/g;
$chunk =~ s/%26/\&/g;
$chunk =~ s/%20/ /g;
$chunk =~ s/%7E/~/g;
$chunk =~ s/%21/\!/g;
$chunk =~ s/%3F/\?/g;
$chunk =~ s/Describe your job or area of study//;

if ($year < 100) {
# dates in email headers are only two digits
    $year += 1900;
}
 @parts = split(/\&/,$chunk);
 %pieces = ();
 foreach $piece (@parts) {
    @smallbits = split('=', $piece);
    $pieces{@smallbits[0]} = @smallbits[1];
 }

 $record = $year."\t".$m."\t".$day."\t";
 foreach $f (@fieldnames) {
    $record .= $pieces{"$f"}."\t";
 }
 chop($record);
 print RESULT "$record\n";
}



------------------------------------------------------
Charles Cave
Sydney, Australia
Email: charles@jolt.mpx.com.au
URL:   http://www.ozemail.com.au/~caveman
------------------------------------------------------