We help IT Professionals succeed at work.

form result email in html format

bogie
bogie asked
on
279 Views
Last Modified: 2013-12-25
Does anyone know of a script that will process the results of an html form and that would send an email that would be in html format and look exactly like the html form, but with the fields filled in as they were when the form was submitted? In that way, the recipient would be able to print out a completed form from the user. Any other ideas would be welcome as well.
Comment
Watch Question

Commented:
is pretty easy to do in PERL. However you must remember that not ALL email clients support HTML tags in their Email. so not everybody can get the HTML like look-and-feel. An alternate would be a neatly formatted text output that would be supported by all the Email clients including Non-GUI ones!!

Author

Commented:
Thank you for your reply. The receipient would only be a single user,and they would like the form fields heading and all... but in any case, do you have a Perl script that can handle this? I can handle html very well, but Perl is not my forte. Would I be able to add/change the fields myself by going into the script and simply adding/changing small snippets?

You can see the form here: http://www.marblelife.com/salesrpt.htm

Thanks, Herb

Commented:
1. make copy of salesrpt.htm file - salessend.htm
2. edit salessend.htm and in every <input > insert text value="%%fieldName%%"
3. create script

#!/usr/bin/perl

use CGI;
$q = new CGI;
open(F, 'salessend.htm");
$sales = join('', <F>);
close(F);

@fields = qw(RESMarbleRest RESGraniteRest all other field names with correct Cappitals );

foreach $f(@fields){
  $pat = '%%'.$f.'%%';
  $sales =~ s/$pat/$q->param($f)/g;
}

open(M, '|/bin/mail -s Subject person@to.send.to');
print M $sales;
close(M);

print $q->header();
print "Report sent\n";

exit 0;

Good Look!

Commented:
oops, line

$sales =~ s/$pat/$q->param($f)/g;

should read

$sales =~ s/$pat/$q->param($f)/ge;

Commented:
yet another typo
open(F, 'salessend.htm");
should be
open(F, 'salessend.htm');

Commented:
try this.

you dont need to modify anything in salesrpt.htm.
it is the starting point for the CGI i am have coded below.

Also you must copy this template file to any location and put the full path to the file in the CGI script.

Basically this is how it works. i have created a mirror hmtl file that acts as a template to salesrpt.htm.

it has all the HTML tags and so on. The only difference is that instead of the input tags i have put the actual variable names in CAPS with a ## before and after the variable. Eg ##RETURN_LINK_URL##, ##WEEK_OF## etc..

NOTE: the variable names MUST be the same as in salesrpt.htm and in all caps.

Now when the user submits the form, the CGI script goes thru the template and just replaces the variables with their actual value!!

this program is flexible so that tomorrow if you were to add/remove new variable(s) to salesrpt.htm, you need to ONLY add/remove the variable in the salesrpt_template.html file!! your CGI can continue to remain unchanged!!

However do remember the NOTE above.


Installation: copy the formmail.pl to your cgi-bin directory, give it proper permissions & remember to changed the full path of the template file
($template_file="/www/sbarney/docs/manesh/salesrpt_template.html";)
to your location.


==========================salesrpt_template.html
<html>
<BODY>

<center><font size="4" color="#000080"><u>##SUBJECT##</u></font></b></CENTER><P
>

<table border=0 cellpadding=4>

<TD><font size="2">For the Week of: </font></TD>
<TD>##WEEK_OF##</TD>
<TD></TD>
<TD></TD>
<TR>

<TD><font size="2">Franchise Name: </font></TD>
<TD>##FRANCHISENAME##</TD>
<TD><font size="2">Franchise Acct: </font></TD>
<TD>##FRANCHISEACCT##</TD>
<TR>

<TD><font size="2">Email Address: </font></TD>
<TD>##EMAIL##</TD>
<TD></TD>
<TD></TD>
<TR>
<TD><font size="2">Total Sales: </font></TD>
<TD>##TOTALSALES##</TD>
<TD></TD>
<TD></TD>
<TR>

<TD><font size="2">Residential:</font></TD>
<TD>##RESIDENTIALSALES##</TD>
<TD></TD>
<TD></TD>
<TR>

<TD><font size="2">Commercial:</font></TD>
<TD>##COMMERCIALSALES##</TD>
<TD></TD>
<TD></TD>
</TABLE>

<b>
<p ALIGN="CENTER"><font size="2">Weekly Residential Sales Analysis</font></p>
</b>
<div align="center">
  <center>
  <table BORDER="1" CELLSPACING="1" CELLPADDING="0" width="100%">
    <tr>
      <td bgcolor="#FFFFFF" align="center">
        <p><font size="2">Marble Restoration</font></td>
      <td bgcolor="#FFFFFF" align="center">
        <p><font size="2">Granite Restoration</font></td>
      <td bgcolor="#FFFFFF" align="center">
        <p><font size="2">Terrazzo Restoration</font></td>
      <td bgcolor="#FFFFFF" align="center">
        <p><font size="2">Ceramic Tile</font></td>
      <td bgcolor="#FFFFFF" align="center">
        <p><font size="2">Vanities &amp; Tables Restored</font></td>
      <td bgcolor="#FFFFFF" align="center">
        <p><font size="2">Clean &amp; Polish</font></td>
    </tr>
    <tr>
      <td align="center">
        <p>##RESMARBLEREST##</p>
      </td>
      <td align="center">##RESGRANITEREST##</td>
      <td align="center">
        <p>##RESTERRAZZOREST##</p>
      </td>
      <td align="center">
        <p>##RESCERAMICTILEREST##</p>
      </td>
      <td align="center">
        <p>##RESVANITIESTABLEREST##</p>
      </td>
      <td align="center">
        <p>##RESCLEANPOLISH##</p>
      </td>
    </tr>
    <tr>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Stone Sealed Interlok</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Stone Sealed Impregnator</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Maintenance Acct.^Òs</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">InterCare Sales</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Step 1 &amp; Step 2 Sales</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Misc.</font></td>
    </tr>
    <tr>
      <td align="center">
        <p>##RESSTONESEALEDINTERLOK##</p>
      </td>
      <td align="center">
        <p>##RESSTONESEALEDIMPREGNATOR##</p>
      </td>
      <td align="center">
       <p>##RESMAINTACCTS##</p>
      </td>
      <td align="center">
        <p>##RESINTERCARESALES##</p>
      </td>
      <td align="center">
        <p>##RESSTEP1AND2SALES##</p>
      </td>
      <td align="center">
        <p>##RESMISCSALES##</p>
      </td>
    </tr>
  </table>
  </center>
</div>
<p align="center"><b><font size="2">Weekly Commercial Sales Analysis</font></p>
</b>
<div align="center">
  <center>
  <table BORDER="1" CELLSPACING="1" CELLPADDING="0" width="100%">
    <tr>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Marble Restoration</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Granite Restoration</font></td>
      <td align="center" bgcolor="#FFFFFF">
      <p><font size="2">Terrazzo Restoration</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Ceramic Tile</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Vanities &amp; Tables Restored</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Clean &amp; Polish</font></td>
    </tr>
    <tr>
      <td align="center">
        <p>##COMMARBLEREST##</p>
      </td>
      <td align="center">
        <p>##COMGRANITEREST##</p>
      </td>
      <td align="center">
        <p>##COMTERRAZZOREST##</p>
      </td>
      <td align="center">
        <p>##COMCERAMICTILEREST##</p>
      </td>
      <td align="center">
        <p>##COMVANITIESTABLEREST##</p>
      </td>
      <td align="center">
        <p>##COMCLEANPOLISH##</p>
     </td>
    </tr>
    <tr>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Stone Sealed Interlok</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Stone Sealed Impregnator</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Maintenance Acct.^Òs</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">InterCare Sales</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Step 1 &amp; Step 2 Sales</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Misc.</font></td>
    </tr>
    <tr>
      <td align="center">
        <p>##COMSTONESEALEDINTERLOK##</p>
      </td>
      <td align="center">
        <p>##COMSTONESEALEDIMPREGNATOR##</p>
      </td>
      <td align="center">
        <p>##COMMAINTACCTS##</p>
      </td>
     <td align="center">
        <p>##COMINTERCARESALES##</p>
      </td>
      <td align="center">
        <p>##COMSTEP1AND2SALES##</p>
      </td>
      <td align="center">
        <p>##COMMISCSALES##</p>
      </td>
    </tr>
  </table>
  </center>
</div><br>
<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
      <td align="center"><font size="2">Column 1</font></td>
      <td align="center"><font size="2">+ Column 2</font></td>
      <td align="center"><font size="2">+ Column 3</font></td>
      <td align="center"><font size="2">+ Column 4</font></td>
      <td align="center"><font size="2">= Column 5</font></td>
    </tr>
  </table>
  </center>
</div>
<div align="center">
  <center>
  <table BORDER="1" CELLSPACING="1" CELLPADDING="0" width="100%">
    <tr>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Previous Acct^Òs Receivable</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Weekly Sales</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Weekly Sales Collected</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Acct^Òs Receivable Collected</font></td>
      <td align="center" bgcolor="#FFFFFF">
        <p><font size="2">Current Acct.^Òs Receivable</font></td>
    </tr>
    <tr>
      <td align="center">
        <p>##PREVAR##</p>
      </td>
      <td align="center">
        <p>##WEEKLYSALES##</p>
      </td>
      <td align="center">
        <p>##WEELKYSALESCOLLECTED##</p>
      </td>
      <td align="center">
       <p>##ARCOLLECTED##</p>
      </td>
      <td align="center">
        <p>##CURRENTAR##</p>
      </td>
    </tr>
  </table>
  </center>
</div>

<P>
<TABLE border=0 cellpadding=2>
<TD><font size="2">6% Royalty Fee of Moneys Collected:</font></TD>
<TD>##SIXPERCENT_AMT##</TD>
<TD><font size="2">CK#</font></TD>
<TD>##ROYALTYFEE##</TD>
<TR>

<TD><font size="2">2% Ad Fund of Moneys Collected:</TD>
<TD>##TWOPERCENT_AMT##</font></TD>
<TD><font size="2">CK#</font></TD>
<TD>##ADFUND##</TD>
<TR>

<TD><font size="2">Total Due MARBLELIFE, Inc: </font></TD>
<TD>##TOTALDUE##</TD>
<TD></TD>
<TD></TD>
<TR>

<TD><font size="2">Total of Weekly Sales Collected and <br>Accounts Receivable
Collected (Column 3 + Column 4): </font></TD>
<TD>##TOTALWEEKLYSALESANDARCOLLECTED##</TD>
<TD></TD>
<TD></TD>
</TABLE>

<p><font size="2">Franchise Signature:</font>_________________________

<P><A HREF="##RETURN_LINK_URL##">##RETURN_LINK_TITLE##</A>

<CENTER><font size="2">Mail Checks and Copies of all Sequentially Numbered Invo
ices to:<br>MARBLELIFE, INC. 805 W. N. Carrier Pkwy, Suite #220, Grand Prairie,
 Texas 75050</font></center>

</body>
</html>
========================formmail.pl
#!/usr/local/bin/perl

use CGI;

$query=new CGI; ##  Create a CGI object to read the values entered in the HTML
form.

##  Full path to the template file.
$template_file="/www/sbarney/docs/manesh/salesrpt_template.html";

print "Content-type: text/html\n\n";
if ($query->param){ ##  This script has been called via a Submit button

  ##  Read every parameter entered in the form...
  ##  ...& store it in an associative array after...
  ##  ...converting the variable to UPPER CASE.
  ##  E.g. if your form variable is called TotalSales, the key will be TOTALSAL
ES and the value against the key will be whatever was entered in the HTML form!
!
  foreach($query->param){
    $key=uc $_;
    $data{$key}=$query->param($_);
  }

  ##  NOTE: DOS files have ^Z as the end-of-file indicator
  $/="^Z";  ##  Make the end-of-line seperator as ^Z
  open(TEMPL,$template_file) || die $!;
 $entire_file=<TEMPL>; ##  Read the ENTIRE file into a PERL variable
  close(TEMPL);
  $/="\n";  ##  Restore the end-of-line seperator to carriage return

  ##  Now, search in the read file for every key in the hash.
  foreach $sp_var (sort keys %data){
    ##  If a match is found, replace the key with its correspoding value.
    $entire_file=~ s/##$sp_var##/$data{$sp_var}/g if $entire_file=~ /##$sp_var#
#/;
  }

  ##  Print the final file. If required, the same can be ...
  ##  ...sent out via an E-mail!!
  print $entire_file,"\n";
}else{  ##  We dont allow the user to directly call this CGI script
  print "<B>You are not allowed here!!</B><br>\n";
}

Commented:
have you been able to test the code that i gave you??

if so, did it work properly??

Author

Commented:
Been out of town all week. Just returned, so I should be able to give it a good going over in the next day or so...but it looks very complete, thanks!
Herb

Commented:
any success so far??

Rgds

Author

Commented:
I am sorry I have been delinquent in working on your answer...we had a family emergency and I have not been able to work on it. Please rest assured that if your answer works, I will let you know and also give a bonus for your patience!
Thanks, Herb

Commented:
i'm sorry. i hope everything is Ok with your family.

please take you time to review the code.

Regards
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.