64x64 icon dark hosting
Choose a hosting plan here and get a free year's subscription to Tuts+ (worth $180).

Quick Tip: Create a Contact Form with SMS and Email Alerts


Start a hosting plan from $3.92/mo and get a free year on Tuts+ (normally $180)

In this Quick Tip, I will show you how to build a contact form using PHP and ActionScript 3. We'll learn how to pass variables from Flash to PHP and alert the owner by sending either an email or an SMS text message.

Step 1: Setting up the Document

The first thing to do is download the source files for this tutorial. Since this tutorial is a Quick Tip, I will skip the layout steps. Once you have the files downloaded, open the 'contactForm.fla' file.

The SWF layout looks like this:


Step 2: The Document Class

Create a new 'ActionScript' file and save it to the same folder as 'contactForm.fla.' Give the file a name of 'contatForm.as.' Next, link the Flash file and ActionScript file together in the Properties panel. For a more in-depth look at how to set up the Document class, check out this quick guide.

Step 3: The Imports

Here are the import statements that we will be using for this file. Also, we will create two global variables. One is to use for a file attachment, and the other is to keep track whether we're going to use that file or not.

Step 4: Starting to Code

The first function we call is init(). Within the function, we set up the labels for our text fields. Also, we create an array of the cell phone providers. Since I'm located in the United States, I use only the U.S. providers. Check out this article on Wikipedia that will help you with the cell phone providers in your country.

We also set up the radio buttons.

Step 5: Handling the Attachment

The first thing we'll worry about is handling the attachment. Since I'm using Flash CS3, we'll do the attaching the old fashioned way. When the 'attach' button is clicked, we will instantiate the FileReference variable that we created. One important thing to note is that the FileReference variable must be a global variable (i.e. not specific to just one function) for events to fire correctly.

When the user has selected a file to upload, we will make the progress bar visible and listen for the 'progress' and 'complete' events. When the 'complete' event fires, we disable the 'attach' button, set the attachment variable to true and hide the progress bar.

Step 6: The Upload Document

Switching over to PHP. First, create a new PHP document and save it as 'upload.php'. Now, create a directory on your web server called 'temp'. This will be the folder that we use to store the uploaded file. We will assign that location to a PHP variable called 'folder'. Next, we use the '$_FILES' super global variable to get the name of the file we uploaded. Then we create a variable that stores the temporary name of the file we just uploaded. Lastly, we move the file into our 'temp' directory. You can find a great tutorial on uploading files with PHP over on Nettuts+.

Step 7: The Message Document

Create a new PHP document and save it as 'message.php'. In this file, we will receive the variables from Flash. The first thing we do is use the '$_POST' super global variable to set the all the email fields such as 'to' and 'from'. Next, we set our 'headers' variable. In PHP, we use '.' instead of '+' to concatenate. On our 'headers' variable, you'll notice '.=' which works the same as '+=' in ActionScript. After each time we concatenate we add two line returns. This is important and without it our email may fail to send.

Finally, after all our headers and fields are ready to go, we call the 'mail' function. If the mail is successful, we check to see if there was a file associated with the email. If the 'file' variable is set, then we simply delete that file from our server using the 'unlink' function.

Step 8: Adding the Attachment

Here is the code that we use to send an attachment. If you want to include this type of functionality in your application, simply insert this code where it says 'FILE ATTACHMENT GOES HERE'. The first thing we do is check to see if the 'file' variable has been posted. Also, we check to see if the 'bool' variable is 'true'. This will check to make sure that the user isn't trying to send an attachment as a text message. Although you can send files as attachments to cell phones, the maximum dimensions are only about 640x480. That would take some extra logic to resize the image and is out of the scope of this Quick Tip. Feel free to try it out yourself, though!

Back on track, we'll create a 'file' variable to leads to the location of the file we uploaded. Also, after doing some simple string replacing, we check the file extension. Finally, we modify our 'headers' variable to include the file as well as all the information that is needed to attach it.

Step 9: Back in Flash

When the 'sender' button is clicked, we call the 'onClick()' function. We perform some checking to see if it is an email or a text message that the user is trying to send. If it's a text message, we'll format the phone number a little, check to see if the user has selected a provider, and make sure the number is valid. After we've validated all of the user's information, we will load the 'message.php' document, send all our variables to it and listen for the 'complete' event.

Step 10: The Event Handlers

When the 'complete' event is fired, we will simply parse the integer that PHP sends us. Along with the 'complete' event, below are the rest of the event handler functions used in this project.

The 'onChange' function will toggle the combo box visibility as well as call the 'handleAddy' function. The 'onText' function will set the focus of the text field when the label is clicked - like in HTML. Finally, the 'onLabel' function will handle the highlighting of the text field when the mouse moves over it.

Step 11: The Rest of the Functions

These are the final functions that will handle checking the text fields to make sure they're not empty as well as handling the offset caused by the radio button selection.


In this Quick Tip, you learned how to use Flash and AS3 to send not only simple text variables but images as well - and not just by email, but also via SMS text message. These techniques can be used in a variety of ways. Keep experimenting and remember to subscribe to the Tuts+ feed. Thanks for reading!