Tutorials Books Videos Forums

Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done
senocular

Flash Based Email Form Using PHP

by senocular   | filed under Flash and ActionScript

Introduction

Here we will cover the process involved in making a very simple email form in Flash using PHP. Be aware that Flash alone is incapable of sending email. PHP, or some other server-side script like PHP, is needed to handle that operation. Put the two together, though, and you got yourself a great way for people to email you directly through your Flash-based web site.

[ send me an email using Flash and PHP ]

How it Works

Because PHP is being used to send an email, Flash needs a way to communicate to PHP to let it know when to send an email and with what information. Flash relies on PHP to handle the email sending responsibility. When a Flash movie is ready to have an email sent, it needs to talk to a PHP script on the server so that script can then instruct the server to send the email with the information provided.

Flash sends data to PHP, and PHP asks the server to send email.

[ Flash movie to PHP to server sending out email ]

A way Flash can send information to an external PHP page is through the loadVariables ActionScript command. This allows Flash to open a page on the server, like a PHP script, and send it specific information that allows you to control how that script behaves. In the case of this tutorial, that means having a PHP script email information inputted in a Flash form.

Flash sends the PHP page the information filled out in the form. The PHP script then inserts that information into a mail function which instructs the server to send an email with that information to the email address specified. Easy enough, right? Let's begin creating the Flash form.

Steps to Create Form

The Flash form consists of two basic parts. One part is the group of text fields that make up the form. These are actually contained within a movie clip aptly named form. Secondly, you have the send button. This will be the button that activates the code that sends the form information to the PHP file. At that point, Flash's job is done. It's then up to the PHP script to make sure the email gets sent.

  1. Start off by making the appropriate form fields. Make sure these are input fields and not static or dynamic text fields. Include as many as you want. Each will be sent to the PHP file where they can then be sorted. This example uses three.
  2. Assign each text field a var value. This is NOT an instance name. The var field allows you to associate a variable with the given text field. Because loadVariables is being used to transmit the information, this is needed to make the value of these text fields easily recognized as variables to that command. This example uses name, email, and body for field variable names.

The var field in Flash.

[ assign a var value for each text field ]

  1. Once you have created and named each field, select them all and create a new movie clip out of them. This will be the form movie clip. Give it the instance name form when you're done.

All text fields grouped in a movie clip named form.

[ all fields in a movie clip named form ]

  1. Next, create a button. This will serve as the send button. This will exist not within the form, but in the same place as the form. It's on this button that the loadVariables script will be added. That script is as follows:
form.loadVariables("email.php", "POST");

This calls loadVariables through the form movie clip, sending all variables saved in that movie clip to email.php using the POST method. Because all the text fields in form have variables associated with them, this effectively sends all information filled out in those fields to the email PHP page. From that PHP page, the sent information can be retrieved using each field's var name.

One thing to be cautious of is that you need to make sure the movie clip you are using loadVariables with exists long enough for it to send its variables to the URL specified. Because this mailer has a thank you screen after sending an email, you'll need to wait until Flash does that before showing that screen and losing the form movie clip.

  1. You can tell when a movie clip has sent its variables out from its onClipEvent(data) event. This event is called when the movie clip receives data from the server either confirming its variables have been sent or in the case where new variables are brought into Flash. We aren't bringing variables in, just sending out. But we do need to know when that's taken place. So on the form movie clip we can add the script that checks for that. When the event runs, we can then go to the next frame showing the thank you screen.
onClipEvent(data) {
  _root.nextFrame();
}

Steps in Scripting PHP

Now we can write that PHP script, email.php, that really makes this happen. Luckily, it's not really all that difficult to write. Maybe not as easy as the previous Flash script, but not difficult nonetheless. The PHP all revolves around one function, the mail function.

All you need to do is grab the information sent from Flash and pass it in to this mail function in PHP to have that information sent to your email of choice.

  1. Create a PHP file if you haven't already. If you're not sure how, just make a text file. A PHP file is basically nothing more than a text file with a .php extension instead of .txt. To a server, though, a PHP file is seen not as text, but as a script for carrying out commands, like our email command.
  2. Using Dreamweaver or your text editor of choice, start writing your PHP script. This will consist of three basic parts. One will be a place for constant variables that will always remain the same. This will include things such as your email. Another is the capturing of those variables sent to the script from Flash. Finally, that information is then setup in the third part, the call of the mail function.
  3. Part 1: Begin the file with a PHP tag and the setting up of constant variables. This includes your email and most likely a subject line as well.
<?php
$sendTo = "[email protected]";
$subject = "My Flash site reply";
  1. Part 2: Now you can start getting the variables that Flash sent. Since Flash sent the variables using post, we would use $_POST to get them into variables of our own. $_POST is a special global variable in a PHP script that contains all of the posted variables sent to that script as an associative array.

Using brackets and a variable name, you can then retrieve those variables. For those variables sent by this particular example, you would obtain their value in PHP using $_POST["name"], $_POST["email"], and $_POST["message"].

$headers = "From: " . $_POST["name"];
$headers .= "<" . $_POST["email"] . ">\r\n";
$headers .= "Reply-To: " . $_POST["email"] . "\r\n";
$headers .= "Return-Path: " . $_POST["email"];
$message = $_POST["message"];

Name and email here are assigned to a single variable, headers, because of how the mail function works.

  1. Part 3: Finally the mail function. This function is set up to take three to five different parameters. We're only concerned about four:
mail(recipient, subject, message, otherHeaders);

With our previous variable definitions, we can easily just plug and play to run this command in the final PHP script. Note that "from" and "reply-to" are not separate parameters within the mail call. These are part of the fourth parameter, additional headers, where information beyond the recipient, subject, and email message go.

With that, we can finish off the script with the following:

mail($sendTo, $subject, $message, $headers);
?>
  1. Save as email.php and you're set.

Putting it All Together

All that remains now is uploading your published SWF, with accompanying HTML, and PHP file to your server. Be sure you keep the PHP file in the same directory as your SWF and HTML or your script may not be found when called from Flash. If you don't want it to be in that same directory, be sure you correctly reference the location of the PHP file in the loadVariables command used in Flash.

Once uploaded, play your movie and send yourself a message! It'll be fun, and it will also test to make sure this actually works. Since, if you didn't already know, you can't test from your own computer's hard drive unless you have the proper configurations and PHP installed, which more than likely is not the case. It's ok though, since you don't need it on your personal computer, just your server. And if it's working from there, then you should be in the clear.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.

Your support keeps this site going!

- senocular

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--