home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / aspfor_1 / formmail.asp < prev    next >
Encoding:
Text File  |  1999-06-16  |  13.9 KB  |  441 lines

  1. <%@ LANGUAGE="JScript" %>
  2.  
  3. <%
  4.  
  5. /*                                 
  6.  *   ASP FormMail (BETA) for IIS 2.0+
  7.  *
  8.  *   Version: v1.1b      
  9.  *   Author : Larry [Josh] Ham  (lham@geocel.com)
  10.  *            Geocel International (c) 1999 
  11.  *
  12.  *   Last Modified: 06/16/99
  13.  *   All rights reserved.
  14.  *   -----------------------------
  15.  *
  16.  *   ASP FormMail is designed to mimic Matt Wright's popular perl FormMail script located
  17.  *   at http://www.worldwidemart.com/scripts/  The most important difference is that
  18.  *   ASP FormMail is designed mainly for IIS and does not require any third party programs
  19.  *   such as Perl or SendMail. It is completely handled internally via your web server.
  20.  *
  21.  *   ASP FormMail has been designed around the server-side mail component "DevMailer". 
  22.  *   However, it may be modified to use other compenents. View the README file for
  23.  *   for more information regarding "DevMailer".
  24.  *
  25.  *   Please send bug reports, comments, etc to lham@geocel.com
  26.  *
  27.  *   ASP FormMail may be used and modified free of charge by anyone so long as this
  28.  *   copyright notice and the comments above remain intact.  By using this code
  29.  *   you agree to indemnify Lawrence [Josh] Ham from any liability that might arise
  30.  *   from its use.                                                  
  31.  *                                                                            
  32.  *   Selling the code for this program without prior written consent is     
  33.  *   expressly forbidden. 
  34.  *
  35.  *   This is a beta release meant for testing only until this line is removed by the author.
  36.  *
  37.  *   ChangeLog:
  38.  *       1.0b - Inital Release
  39.  *       1.1b - Add: a FILES hidden input tag to allow for easy addition
  40.  *              of user select file attachments.
  41.  *              Add: Included a copy of DVMAILER.DLL (the mailer object)
  42.  *              Fix: Notification when EMAIL tag is missing. (this was
  43.  *                   required)
  44.  */                                
  45.  
  46. Response.Expires = 0;
  47.  
  48. ConfigOptions = new Array(
  49.   'RECIPIENT',
  50.   'SUBJECT',
  51.   'REALNAME',
  52.   'REDIRECT',
  53.   'BGCOLOR',
  54.   'BACKGROUND',
  55.   'LINK_COLOR',
  56.   'VLINK_COLOR',
  57.   'TEXT_COLOR',
  58.   'ALINK_COLOR',
  59.   'SORT',
  60.   'PRINT_CONFIG',
  61.   'REQUIRED',
  62.   'ENV_REPORT',
  63.   'RETURN_LINK_TITLE',
  64.   'RETURN_LINK_URL',
  65.   'PRINT_BLACK_FIELDS',
  66.   'MISSING_FIELDS_REDIRECT'
  67. );
  68.  
  69.  
  70. /* 
  71.  *   Add your mail server here. If your mail server does not allow message relaying (most do not)
  72.  *   then your recipient (set in your HTML form) account must exist on this server.
  73.  */
  74.  
  75. MailServer = "mailhost.geocel.com";
  76.  
  77. /*
  78.  *   The default mail server port. Doubtful it needs to be changed.
  79.  */
  80.  
  81. SMTPPort = 25;
  82.  
  83. /*
  84.  *   If you do not wish to check referers simply uncomment (put //) in front of the next  *   line.
  85.  */
  86.  
  87. checkReferers = true;
  88.  
  89. /*
  90.  *   Referers allows forms to be located only on servers which are defined  
  91.  *   in this field.  This security fix from the last version which allowed  
  92.  *   anyone on any server to use your FormMail script on their web site.
  93.  */
  94.  
  95. Referers = new Array('208.24.33.19', 'www.geocel.com');
  96.  
  97. /*
  98.  *    Done with Configuration variables.
  99.  */
  100.  
  101.  
  102. CheckRequired();
  103. CheckRecipient();
  104. CheckEMail();
  105. CheckReferers();
  106.  
  107. SendMail();
  108.  
  109. %>
  110.  
  111. <%
  112.     function SendMail() {
  113.         DM = Server.CreateObject('Geocel.Mailer');
  114.  
  115.         /*
  116.          *  If the above line fails, run the following from the
  117.          *  the command line 'C:\>' - REGSVR32.EXE DVMAILER.DLL
  118.          */
  119.  
  120.         body = "";
  121.         if( Request('Subject').Count )
  122.             sub = String(Request('Subject'));
  123.         else
  124.              sub = "ASP FormMail Results";
  125.         if( Request('RealName').Count )
  126.              from_name = String(Request('RealName'));
  127.         else
  128.              from_name = 'DevMailer';
  129.  
  130.         if( Request('EMail').Count )
  131.              from_email = String(Request('EMail'));
  132.         else
  133.              from_email = "WWW Web Submission";
  134.  
  135.         DM.AddServer(MailServer, SMTPPort);
  136.         DM.ContentType = "text/html";
  137.         DM.Priority = 1;
  138.         DM.Subject = sub;
  139.         DM.FromName = from_name;
  140.         DM.FromAddress = from_email;
  141.         DM.AddRecipient(Request('Recipient'), '');
  142.  
  143.         /* Add any CC Recipients here and un-comment lines */
  144.  
  145.         // DM.AddCCRecipient("lham@geocel.com","Larry [Josh] Ham");
  146.         // DM.AddCCRecipient("josh@geocel.com","Josh");
  147.  
  148.         /* Add any CC CRecipients here and un-comment lines */
  149.  
  150.         // DM.AddBCCRecipient("lham@geocel.com","Larry [Josh] Ham");
  151.         // DM.AddBCCRecipient("josh@geocel.com","Josh");
  152.  
  153.         attach = Request('FILES').Count;
  154.         if( attach ) {
  155.             // look for multiple attachments (comma-seperated) - split them apart
  156.             // into an array.
  157.             mfiles = String(Request('FILES')).split(','); 
  158.             a = mfiles.length;
  159.  
  160.             filePath = 'C:\\ATTACH\\';
  161.  
  162.             /* Change this to a directory where your file attachments
  163.              * are stored.
  164.              */
  165.  
  166.             while( a-- ) {
  167.                 DM.AddAttachment( filePath + mfiles[a] );
  168.             }
  169.         }
  170.  
  171.         sort = Request('SORT').Count ? Request('SORT') : "";
  172.         elements = null;
  173.         esize = 0;
  174.         if( sort == "alphabetic" || !sort.length ) {
  175.             elements = new Array(Request.Form.count);
  176.             en = new Enumerator(Request.Form);
  177.             en.moveFirst();
  178.             esize = 0;
  179.             while( !en.atEnd() ) {
  180.                 elements[ esize ] = String( en.item() ).toUpperCase();
  181.                 esize ++;
  182.                 en.moveNext();
  183.             }
  184.          }
  185.          else {
  186.              elements = String(Request('SORT')).split(',');
  187.              esize = elements.length;
  188.          }
  189.          if( sort == "alphabetic" ) {
  190.              elements.sort();
  191.              elements.reverse();
  192.          }
  193.          while( esize-- ) {
  194.              if( !isOption( elements[ esize ] ) ) {
  195.                  form = Request( elements[esize] );
  196.                  blank = !form.Count || String(form).length < 1;
  197.                  if( blank ) {
  198.                      if( !Request('print_blank_fields').Count ) continue;
  199.                  }
  200.                  body += ('<b>' + elements[ esize ] + '</b> : ' + Request(elements[esize]) + '<br>\r\n');
  201.               }
  202.          }
  203.          DM.Body = body;
  204.          DM.Send();
  205.  
  206.          if( Request('REDIRECT').Count ) {
  207.              Response.Redirect(Request('REDIRECT'));
  208.          }
  209.  
  210.          PrintHeader(Request('TITLE').Count ? Request('TITLE') : 'Thank You');
  211.          htitle = Request('TITLE').Count ? Request('TITLE') : 'Thank You for filling out this form.';
  212.          d = new Date();
  213. %>
  214.  
  215.           <TABLE border=0 width="75%" bgcolor=#CCCCCC align="center">
  216.           <TR>
  217.             <TH>
  218.                 <FONT size=+2><%=htitle%></FONT>
  219.             </TH>
  220.           </TR>
  221.           </TABLE>
  222.           <TABLE border=0 width="75%" bgcolor=#99cccc align="center">
  223.           <TR>
  224.               <TD align="left" width="5%">
  225.                 O
  226.               </TD>
  227.               <TD>
  228.                   Below is what you submitted to <B><%=Request('RECIPIENT')%></B> on 
  229.                   <%=d.toLocaleString()%>
  230.               </TD>
  231.           </TR>
  232. <%
  233.            esize = elements.length;
  234.            ecnt = 0;
  235.            while( esize-- ) {
  236.            if( isOption(elements[esize]) ) continue;
  237.            form = Request( elements[esize] );
  238.            blank = !form.Count || String(form).length < 1;
  239.            if( blank ) {
  240.                if( !Request('print_blank_fields').Count ) continue;
  241.            }
  242.            ecnt ++;
  243.            ecnt % 2 ? bgcol = "#FFFFFF" : bgcol="#99cccc"; %>
  244.            <TR>
  245.              <TD bgcolor=<%=bgcol%> width="5%">
  246.                O
  247.              </TD>
  248.              <TD bgcolor=<%=bgcol%>>
  249.                <B><%=elements[ esize ]%></B>: <%=Request(elements[esize])%>
  250.              </TD>
  251.            </TR>
  252. <%
  253.        }
  254. %>
  255.           </TABLE>
  256.           <BR>
  257.           <CENTER>
  258.           <P><HR size=1 width=75%>
  259.           </CENTER>
  260.           <BR>
  261. <%  if (Request('RETURN_LINK_URL').Count && Request('RETURN_LINK_TITLE') ) { %>
  262.           <CENTER>
  263.       <UL>
  264.       <LI>
  265.           <A href="<%=Request('RETURN_LINK_URL')%>"><%=Request('RETURN_LINK_TITLE')%></A>
  266.           </UL>
  267.       </CENTER>
  268. <%
  269.     }
  270.     PrintFooter();
  271. }
  272.  
  273. %>
  274.  
  275. <%
  276.     function isOption(option) {
  277.         oc = ConfigOptions.length;
  278.         option = option.toUpperCase();
  279.         while( oc-- )
  280.             if( ConfigOptions[oc] == option ) return true;
  281.         return false;
  282.     }
  283. %>
  284.  
  285.  
  286. <%
  287.     function PrintHeader(title) { %>
  288.         <HTML>
  289.         <HEAD>
  290.         <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
  291.         <TITLE><%=title%></TITLE>
  292.         </HEAD>
  293.         <BODY
  294. <%
  295.         if( Request('BGCOLOR').Count ) { %> BGCOLOR="<%=Request('BGCOLOR')%>" <% }
  296.         else { %> BGCOLOR="#EEEEEE" <% }
  297.         if( Request('BACKGROUND').Count ) { %> BACKGROUND="<%=Request('BACKGROUND')%>" <% }
  298.         if( Request('LINK_COLOR').Count ) { %> LINK="<%=Request('LINK_COLOR')%>" <% }
  299.         if( Request('VLINK_COLOR').Count ) { %> VLINK="<%=Request('VLINK_COLOR')%>" <% }
  300.         if( Request('ALINK_COLOR').Count ) { %> ALINK="<%=Request('ALINK_COLOR')%>" <% }
  301.         if( Request('TEXT_COLOR').Count ) { %> TEXT="<%=Request('TEXT_COLOR')%>" <% }
  302. %>
  303.         >
  304. <%
  305.     }
  306. %>
  307.  
  308.  
  309. <% 
  310.     function PrintFooter() {
  311. %>
  312.         <BR>
  313.         <TABLE align="CENTER" border=1 width="75%" bgcolor=#669999>
  314.         <TR>
  315.             <TD align="center">
  316.             <FONT size=-1>
  317.             <P>
  318.                   ASP FormMail v1.1b © 1999  GeoCel International<br>
  319.                   GeoCel International<br>
  320.           <A href="http://www.geocel.com">http://www.geocel.com</a><br>
  321.            </FONT>
  322.            </TD>
  323.         </TR>
  324.         </TABLE>
  325.         </BODY>
  326.         </HTML>
  327. <%
  328.     }
  329. %>
  330.  
  331. <%
  332.     function PrintError(error_title, error_message) {
  333.         PrintHeader(error_title);
  334. %>
  335.         <CENTER>
  336.         <TABLE border=0 width="75%" bgcolor=#9C9C9C>
  337.         <TR>
  338.         <TH>
  339.             <FONT size=+2><%=error_title%></FONT>
  340.         </TH>
  341.         </TR>
  342.         </TABLE>
  343.         <TABLE border=0 width="75%" bgcolor=#CFCFCF>
  344.         <TR>
  345.         <TD>
  346.           <%=error_message%>
  347.         </TD>
  348.     </TR>
  349.         </TABLE>
  350. <%
  351.         PrintFooter();
  352.     Response.End();
  353.     }
  354. %>
  355.  
  356.  
  357. <%
  358.     function CheckRequired() {
  359.         if( !Request('REQUIRED').Count ) return;
  360.         req = String(Request('REQUIRED')).split(',');
  361.         bad  = "";
  362.         for( rx = 0; rx < req.length; rx ++ ) {
  363.             if( !Request( req[rx] ).Count || String( Request(req[rx]) ).length < 1 ) {
  364.                 if( bad.length ) bad += ",";
  365.                 bad += String( req[rx] );
  366.             }
  367.         }
  368.         if( bad.length ) {
  369.             RequiredError( bad );
  370.         }
  371.     }
  372.  
  373.     function CheckReferers() {
  374.         ref = String(Request('HTTP_REFERER')).toLowerCase();
  375.         if( !ref.length ) return;
  376.         else if( !checkReferers ) return;
  377.         refs = ref.split('/');
  378.         for( refc = 0; refc < refs.length; refc ++ ) {
  379.             rcnt = Referers.length;
  380.             while( rcnt-- ) {
  381.                 rtmp = String( Referers[rcnt] ).toLowerCase();
  382.                 if( rtmp == refs[refc] ) return;
  383.              }
  384.          }
  385.          PrintError('Bad Referrer - Access Denied',
  386.                     'The form attempting to use ASP FormMail resides ' +
  387.                     'at <tt>' + Request.ServerVariables('HTTP_REFERER') + '</tt>, which is ' +
  388.                     'not allowed to access this ASP page.<p>' +
  389.                     'If you are attempting to configure ASP FormMail to run ' +
  390.                     'with this form, you need to add the following to Referers, ' +
  391.                     'explained in detail in the README file.<p>' +
  392.                     'Add <tt>' + Request.ServerVariables('HTTP_HOST') + '</tt> to your <tt>' +
  393.                     '<b>\Referers</b></tt> array.');
  394.     }
  395.  
  396.     function CheckRecipient() {
  397.         if( Request('RECIPIENT').Count ) return;
  398.         else {
  399.             PrintError('ERROR: Missing EMail Recipient',
  400.                        'No Recipient was specified in the data sent to ASP FormMail.  Please ' +
  401.                        'make sure you have filled in the "RECIPIENT" form field with an e-mail ' +
  402.                        'address.  More information on filling in recipient form fields can be  ' +
  403.                        'found in the README file.');
  404.         }
  405.     }
  406.  
  407.     function CheckEMail() {
  408.         if( !Request('EMAIL').Count ) return;
  409.         part1 = String(Request('EMAIL')).split('@');
  410.         if( part1.length > 1 ) {
  411.             part2 = String( part1[1] ).split('.');
  412.             if( part2.length > 1 ) return;
  413.         }
  414.         PrintError('ERROR: Invalid EMail Address',
  415.                    '<br>The email address "' + Request('EMAIL') + '" is invalid. Please ' +
  416.                    'use your browsers back button to return to the form and try again.<br>');
  417.     }
  418.  
  419.     function RequiredError( missing_fields ) {
  420.         mfr = Request('MISSING_FIELDS_REDIRECT').Count;
  421.         re_text = "";
  422.         if( mfr ) {
  423.             mfr_location = Request('MISSING_FIELDS_REDIRECT');
  424.             Response.Redirect( mfr_location );
  425.          }
  426.          else {
  427.              re_text = "";
  428.              mxa = missing_fields.split(',');
  429.              for( mxc = 0; mxc < mxa.length; mxc ++ ) { 
  430.                  re_text += '<LI> ' + mxa[ mxc ];
  431.               }
  432.       }
  433.          PrintError('ERROR: Missing Fields', 
  434.            'The following fields were left blank in your submission form:<p> ' +
  435.                    '<ul>' + re_text + '</ul><br>' +
  436.                    'These fields must be filled in before you can successfully submit the form.<p>' +
  437.                    'Please use your browser\'s back button to return to the form and try again.' +
  438.            '<hr size=1>');
  439.      }
  440. %>
  441.