Build a Mail Form with Flash and PHP
This tutorial will demonstrate how to build a Flash movie which will, with some help from PHP, send an email message. This tutorial uses Macromedia Flash MX (not MX 2004, although updated project files are available for Flash MX 2004).
Here’s a demo of what we’ll be building in this tutorial. It’s perfectly safe to play with — I’m not harvesting email addresses or anything sinister like that, so feel free to experiment with your own address. The send mail function is disabled but otherwise it is fully featured, including the validation routine. Try clicking the “send” button with some fields empty. Try leaving out the “at” symbol from your email address. Try deliberately typing a fraudulent email address. Refresh your browser page to reset the form.
There are advantages to replacing simple html “mailto:” links with a form which enables your site’s visitors to send you an email.
- First, it is a nice touch. Providing a clean, simple page integrated with your site design instead of launching a separate email client allows the visitor to stay in your site without distractions.
- Second, a form allows you some degree of control over correspondence generated from your site — you can elect to have mail sent to a specific address. If you choose to, you can have the subject of the email hard coded into the script, which can make it much easier to identify email which is coming directly from your site.
- Third, using a form permits you to remove all links to your email address which could be collected by an indexing spider, hopefully reducing spam generated by these agents.
- Finally, the rich visual environment of Flash makes it a little bit easier to craft the appearance of your mail module and a lot easier to give your visitors feedback as to what is happening after they click the “submit” button.
Project file downloads for Flash MX and Flash MX 2004 are available at the end of page five.
There are some excellent resources on the internet which demonstrate how to create a form in html whose “action” calls a cgi or PHP script. One I can recommend from first hand experience is Kuckus’ Contact Page, which uses PHP.
Obviously if you have a site that is predominately or entirely built in Flash it is undesirable to have to go out to a html page to provide an email form. It turns out not to be very hard to build the equivalent of a mail form in Flash MX. Although Flash supports “mailto:” links, Flash by itself isn’t capable of actually sending mail. Fortunately PHP does have this capability and even more fortunately Flash and PHP communicate with each other exceptionally smoothly.
This tutorial contains very little original material by me. Rather, it is a synthesis that seeks to combine what I feel are the best elements of several authors’ works into one optimized package. The three major sources I need to acknowledge are:
- Alessandro Crugnola (Sephiroth), whose excellent tutorial on creating a Flash Email Form was the conceptual inspiration for my tutorial and the source of several useful functions.
- Trib’s excellent PHP tutorial on “Talking GeekSpeak to a Mailbox” which provides the basis for the email validation in the PHP file.
- Colin Moock’s book Actionscript For Flash MX, the Definitive Guide which provided details on the LoadVars methods.
This tutorial will examine the PHP and Flash elements separately.
Page Two covers building the PHP processEmail.php server script.
Page Three covers creating the graphics and text fields for the Flash module
Page Four covers the ActionScript that ties everything together.
Page Five concludes with two nice User Interface refinements. (Links to project file downloads for Flash MX and Flash MX 2004 are also available at the end of page five.)
Page Six covers expanding your form, enabling it to send additional text input