mail form

source: http://www.thegoldenmean.com

The PHP script

It is assumed that the reader has access to a web server running Apache (see Trib’s notes on Windows server software issues) on which PHP is enabled. Server scripts will not work unless they are called from and processed by a server.

The PHP script needs to do four things:

The Flash movie is going to send four variables to the PHP script via the POST method:

If you intend to use this Mail Form to send non-English or accented text characters, please read the addendum “International Characters” below.

The PHP script starts by assigning short variable names to the data it has received:

<?php
	$name = $_POST['name'];
	$email = $_POST['email'];
	$subject = $_POST['subject'];
	$message = $_POST['message'];

(If you find your script isn’t working, it is possible your server is running an older build of PHP. Try replacing the short variable name syntax with the older but possibly safer syntax. For example: $name = $HTTP_POST_VAR['name'];
Additional information concerning the predefined $_POST variable and its uses is available at the Online Manual.)

It then does some minimal string processing:

$name=trim($name);
   $email=trim($email);
   $subject=stripslashes($subject);
   $message=stripslashes($message);

If you are interested in pursuing the two PHP functions I just casually tossed off in the code block above, the Online Manual is once again the difinitive source: stripslashes and trim

Then the recipient’s email address (that’s you!) is hard coded:

	$toaddress='you@YourSite.com';

(You must modify the preceeding line, replacing what I have with your email address. You knew that, right?)

If you opt to hard code the subject (to make it easier to spot mail coming from your site’s mail form, distinguishing it from any other mail), then just eliminate

$subject = $_POST[subject];

and replace it with

$subject = 'A Message From My WebSite';

Please Read:
In the years since Trib wrote the e-mail validation code that follows, many ISP admins have evidently implemented security measures that cause this validation to fail. In response to a stream of e-mails from frustrated readers I am reluctantly recommending that you ignore the remainder of this page and skip directly to the simplified mail script (or the “International Characters” example which likewise omits the validation script). I am keeping the .fla construction intact in hopes of one day getting a more contemporary validation script. The simplified script does not validate but it does seem to send mail reliably and works well with the Flash movie.

Next comes the validation routine. I refer you to the tutorial by Trib, partly because he is a much more entertaining writer than I, partly because it would be redundant for me to cover the same material. Unless you already know all about how email servers work, I really encourage you to read the whole tutorial. It is very interesting material, presented very well indeed.

In short, Trib’s function first evaluates the variable “$email” with a regular expression to check whether it meets the basic form of a valid email address. If it fails that test, the function returns “2” and exits. If it passes that test, the function goes on to actually interrogate the sender’s mail box to determine if it is a valid mail box willing to accept email. If not, it again returns “2” and exits.

If it passes that but encounters a problem, the function returns “1” (a System Error) and exits.

Finally if all seems fine, the function returns “0” (“go ahead”).

After reading Trib’s tutorial and downloading Trib’s validation function, insert that code after the variable declarations in your processMail.php script. Please be an ethical person and keep Trib’s credit in the script.

(Note that in his tutorial and in the script you will get when you click the link in the previous paragraph Trib uses the variable name “$Email” but I have changed it to “$email” because that’s more consistent with how I write variables. If you are going to do things my way remember to change all instances of the variable in the validation script! Also note that I call the function validate_email(), so be sure to change the function which had been named validateEmail(). Do what you are comfortable with but remember that variables are case sensitive and they need to be consistent throughout all parts of the script!)

The PHP script concludes by calling the function and echoing one of three responses based on the outcome of the validation:

$valid = validate_email($email);

switch ($valid) {

  case 0:
    mail($toaddress,$subject,$message,"From: $name <$email>");
     //clear the variables
     $name='';
     $email='';
     $subject='';
     $message='';
     echo 'response=passed';
     break;
  case 1:
     echo 'response=error';
     break;
  case 2:
     echo 'response=invalid';
     break;

}//end switch 
?>

In each case, the script echoes a variable named “response” for Flash:

Note that in the final case the mail() function is actually called and the variables are cleared.

Save, and name your script “processEmail.php”. That does it for the PHP portion. Be sure that when you get to the testing phase you remember to upload processEmail.php along with your swf file!

Simplified E-mail code

Modify the script below to contain your e-mail address and save the file as “processEmail.php”. Make sure you save it in the same directory as the Flash movie. (Alternatively, see the “International Characters” version which alos omits validation.)

<?php

//create short variable names
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
$name=trim($name);
$email=trim($email);
$subject=StripSlashes($subject);
$message=StripSlashes($message);
//modify the next line with your own email address
$toaddress='yourAddress@yourDomain.com';


mail($toaddress,$subject,$message,"From: $name <$email>");
//clear the variables
$name='';
$email='';
$subject='';
$message='';
echo "response=passed";
	
?>

International Characters

Flash MX’s LoadVars class is designed to send strings to a server script encoded in one or more hexadecimal sequences which represent the character’s UTF-8 (also known as unicode) sequence. PHP 4.x is designed to expect incoming data to be in the form of a hexadecimal sequence representing the character’s code point using the system code page, and does not by default understand unicode. This poses no problems for those of us who communicate in English. The standard numeric digits 0-9 and unaccented Latin characters a-z and A-Z aren’t affected.

Obviously a large portion of the world communicates with alphabets containing characters that extend beyond what ASCII text was designed to encompass. This was brought to my attention by a French speaker who discovered that the basic Flash/PHP Mail Form mangled letters with diacritical marks such as é, á or ç.

It turns out there is a solution, namely the utf8_decode() function. Using utf8_decode by itself seems to eliminate problems with languages whose alphabets extend into single-byte unicode glyphs (I have confirmation that this includes French, German and Swedish). If your language extends into double-byte (or even triple-byte) unicode glyphs, read some of the solutions proposed in the PHP Manual’s utf8_decode() entry.

Implementing utf8_decode in the Flash/PHP Mail Form involves a simple modification of the processEmail.php script. Note that this has no ill effect on English characters so it might be worth doing even if you are an English speaker. Who knows - you too might one day be contacted by an individual whose name contains French characters! A basic, non-validating example of the modified processEmail.php script follows:

<?php

//create short variable names
$name=utf8_decode($HTTP_POST_VARS['name']);
$email=utf8_decode($HTTP_POST_VARS['email']);
$subject=utf8_decode($HTTP_POST_VARS['subject']);
$message=utf8_decode($HTTP_POST_VARS['message']);
$name=trim($name);
$email=trim($email);
$subject=StripSlashes($subject);
$message=StripSlashes($message);
$toaddress='YOUR_EMAIL_GOES _HERE;
$returnAddr="From: $name <$email>\nX-Mailer: PHP/".phpversion();

mail($toaddress,$subject,$message,$returnAddr);
   //clear the variables
   $name='';
   $email='';
   $subject='';
   $message='';
   echo 'response=passed';	
?>

Page Three details how to build the Flash movie.

divider ornament

--top--