Flash Contact Form That Sends Mail
Learning how to interact with server-side controls from Flash is very useful. Flash can make a GUI look interactive, awesome, and elegant. Server side controls can persist data, send emails, and do other powerful things. Tying the two together can make magical things happen.
The URLRequest and URLLoader that ActionScript supplies makes calling a server side control easy as pie. I mention in the video, or at least attempt to mention, the URL the flash application is accessing/sending data to needs to be on the same domain that the flash application is on. This is for security reasons.
Below is the code I promised - 100% working. It would be awesome to add some effects on the flash form. Like once the mail gets sent; have a big monster come and pulverize the text inputs rendering them useless.
Flash Class:
package {
import flash.display.MovieClip;
import fl.controls.Button;
import fl.controls.TextInput;
import fl.controls.TextArea;
import fl.controls.Label;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoader;
import flash.net.URLRequestMethod;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.Event;
public class MailScript extends MovieClip {
private var submit:Button;
private var theName:TextInput;
private var theEmail:TextInput;
private var theComment:TextArea;
private var nameLabel:Label;
private var emailLabel:Label;
private var commentLabel:Label;
private var successLabel:Label
public function MailScript() {
buildControls();
}
private function sendMail(e:MouseEvent):void {
var url:String = "http://www.robkeplin.com/sendMailTest.php";
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.senderName = theName.text;
variables.senderEmail = theEmail.text;
variables.senderComment = theComment.text;
request.data = variables;
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
try {
loader.load(request);
} catch(e:SecurityError) {
trace("Security Error...");
}
loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.addEventListener(Event.COMPLETE, successHandler);
}
private function successHandler(e:Event):void {
try {
trace(e.currentTarget.data);
if(e.currentTarget.data == "1") {
submit.removeEventListener(MouseEvent.CLICK, sendMail);
successLabel.text = "Successfully Sent Mail!";
} else if(e.currentTarget.data == "3") {
successLabel.text = "Error - Email Must Be Valid!";
} else if(e.currentTarget.data == "4") {
successLabel.text = "All fields are required!";
} else {
successLabel.text = "Sorry, Unable to Send Mail... :(";
}
} catch(error:TypeError) {
trace("Error getting loader data");
}
}
private function errorHandler(e:IOErrorEvent):void {
trace("Whoopsies, problem loading the page...");
}
private function buildControls():void {
submit = new Button();
theName = new TextInput();
theEmail = new TextInput();
theComment = new TextArea();
nameLabel = new Label();
emailLabel = new Label();
commentLabel = new Label();
successLabel = new Label();
submit.x = 100;
successLabel.x = 200;
theName.x = 100;
theEmail.x = 100;
emailLabel.x = 30;
theComment.x = 100;
nameLabel.x = 30;
commentLabel.x = 30;
theName.y = 17;
nameLabel.y = 17;
theEmail.y = 45;
emailLabel.y = 45;
theComment.y = 75;
commentLabel.y = 75;
submit.y = 180;
successLabel.y = 180;
theName.width = 200;
theEmail.width = 200;
theComment.width = 300;
theComment.height = 100;
nameLabel.text = "Name: ";
emailLabel.text = "Email: ";
commentLabel.text = "Comment: ";
submit.label = "Send!";
successLabel.text = "";
successLabel.autoSize = "left";
submit.addEventListener(MouseEvent.CLICK, sendMail);
addChild(submit);
addChild(theName);
addChild(theEmail);
addChild(theComment);
addChild(nameLabel);
addChild(emailLabel);
addChild(commentLabel);
addChild(successLabel);
}
}
}
The PHP processing page:
<?php
/**
* @author Rob Keplin
* @copyright 2009
*/
if($_POST['senderName'] && $_POST['senderComment'] && $_POST['senderEmail']) {
if(preg_match("/^[a-zA-Z0-9-._]+@[a-zA-Z0-9-._]+.(com|org|net|mil|edu)/", $_POST['senderEmail']))
{
$recipient = "your-email@example.com";
$message = nl2br($_POST['senderComment']);
$message .= "rnFrom: " . $_POST['senderName'];
$subject = "From My Flash App!";
$headers = "From: " . $_POST['senderEmail'];
if(mail($recipient, $subject, $message, $headers)) {
echo "1"; //Success
} else {
echo "2"; //Failed Sending Mail
}
} else {
echo "3"; //Invalid Email
}
} else {
echo "4"; // All Fields Required
}
?>



snir - February 16th, 2009 @ 6:31am reply
Mike Ness - March 8th, 2011 @ 9:52pm reply
I would like to genuinely help you get a lot more exposure by improving your Alexa rank.
I work with a Silicon Valley company called "Alexage.com" that specialises in Alexa rank improvement, and I invite you to check out our Alexa ranking packages at www.alexage.com.
You can read there the importance of having a good Alexa rank and how it could majorly impact your site in positive way.
Let me know if you have any questions.
Best regards,
Mike Ness
mike@alexage.com
Ady - March 19th, 2009 @ 9:55am reply
adisan - April 30th, 2009 @ 11:10am reply
Nishanth - May 25th, 2009 @ 3:00am reply
Mike - June 30th, 2009 @ 6:54am reply
Joe Dassler - July 7th, 2009 @ 11:00pm reply
Rob - July 24th, 2009 @ 1:09pm reply
Vsevolod - July 28th, 2009 @ 11:02am reply
Eduardo - September 19th, 2009 @ 4:19pm reply
Rob - September 26th, 2009 @ 6:46pm reply
Michael - October 27th, 2009 @ 7:23pm reply
Doug - March 14th, 2010 @ 12:10pm reply
Rob - April 13th, 2010 @ 11:47am reply
Kurt - April 19th, 2010 @ 2:11pm reply
Rob - April 22nd, 2010 @ 8:17am reply
amber ruiz - May 25th, 2010 @ 3:15pm reply
So do i cut and past the entire source code including the processing page into an action script page? i already have a flash form page built i just need to be able to send it so i can get E-Mail Responses.can you please help? Thanks Again
Amber Ruiz....... In Case 562 412-7393
Rob - May 25th, 2010 @ 6:41pm reply
The processing page won't go in your flash project, as it's a PHP script. But, it will need to go on your server. Make sure you change the $recipient variable to your own email address.
You'll then need to create event listeners in your current form. The goal is to create a URL request to the PHP script, as outlined in the sendMail() method.
amber ruiz - May 25th, 2010 @ 7:43pm reply
i just used the components to create the form page in flash.
Rob - May 26th, 2010 @ 4:43pm reply
Copy/paste the script from my blog, and into your favorite text editor. Save it as "send-mail.php". Then upload the file to your server, the same way you uploaded your .swf files.
Make sure that you change "rkeplin@gmail.com" to whatever email you wish them to go to.
To verify that you've done this correctly, visit the page, http://www.teamizentertainment.com/send-mail.php, and you should see the number 4.
As for the flash part, I'd recommend reading up on how to attach mouse event listeners to your components. Once you're comfortable with using mouse events, things will get easier.
Once you've mastered them, set up a mouse event that makes a URL request to send-mail.php, and you're done! As always, you can use my flash class above as a reference.
amber ruiz - May 26th, 2010 @ 5:16pm reply
Michael - May 26th, 2010 @ 10:46pm reply
Thanks for the post, it is most helpful. I do have a question, and would really appreciate any help.
I am making a Flash site with my own contact form, which I constructed with my own text input fields. Rather than making a class, I just pasted all your code into the fla file itself, removing "private" and "public", but keeping the same functions and instance names. When I run a "test movie" on flash, I am always getting the 3 error (invalid email). If I leave any fields blank, I do get the 4 error. So I imagine the functions and php are working ok, because I do get these 2 different errors.
Here's the thing. When I trace out (variables.senderEmail) I get the correct email in the output window, just as typed. For example: john@johndoe.com But when I trace (variables), it spits out all the data, but senderEmail shows up with a %40 instead of the @ symbol, thus: john%40johndoe%2Ecom. I am not sure if this is the problem or not, but it's the only thing I can think of. Is this all caused because I did not define a class? Or is there some other problem? Hope to hear from you, and thanks again.
Rob - May 27th, 2010 @ 6:46am reply
if(preg_match("/^[a-zA-Z0-9\-\.\_]+\@[a-zA-Z0-9\-\.\_]+\.(com|org|net|mil|edu)/", $_POST['senderEmail']))
If that doesn't work, try this:
if(preg_match("/^[a-zA-Z0-9\-\.\_]+\@[a-zA-Z0-9\-\.\_]+\.(com|org|net|mil|edu)/", urldecode($_POST['senderEmail'])))
I think this should fix it. Let me know.
Michael - May 27th, 2010 @ 11:48am reply
First of all, I do finally get an echo of "1" on my "test movie." I do not receive an email when trying through a flash "test movie," nor should I, right?
In any case, when I try it online through IE and Safari, it does tell me that the Email was sent Successfully. However, I never actually receive an email, even though I did change the recipient address to my own.
Also, funny enough, when I try it on Firefox, as soon as I click on the submit button it just sits there "Transferring data from mywebsite.com..." and never actually loads a success statement or anything. After 5 minutes it stops trying to load, but with no label statement.
I get all of these same results using both lines of code that you posted above for the PHP.
Again, because the echo system seems to be working, I have to believe that it is ok that I didn't define a class and package and all that. But if that were the problem, I am not sure how to handle it. Is this the method?
1. Write a Contact.as file, purley actionscript from your code above (minus the buildControls), and use its folder location as the package name.
2. Go back to my fla file, select my custom text fields and buttons, and convert it to a Contact movie clip
3. Under its properties > Linkage, export for ActionScript, class = Contact, Base class = package folder.Contact.as ? This is all a guess, and I may be way off.
Thanks again, Rob.
Michael - May 27th, 2010 @ 1:44pm reply
I went and duplicated your MailScript class and created a new contact page with your exact buildControls, in other words a clone of your example, and I posted it to:
http://www.siegelfineart.com/contact_test.html
The form worked without issue.
Perhaps I will try using the class definition after all. I still wonder how I might do this with my own text input fields. I might try it the way I explained above (only I realize my mistake, that the class is MailScript, and not Contact). My custom contact page also has a checkbox, so I will try to figure out how to add variables for that case. Any advice? I will let you know how my trials go. Thanks again for the attention, Rob.
Michael - May 29th, 2010 @ 7:36pm reply
This should interest you. I re-worked everything a hundred times trying to get this to work, and it DOES WORK...BUT!
But what? I realized that my problem the entire time is that my website will do the job PERFECTLY if the user has typed in www before my address (www.siegelfineart.com). However, if the user simply goes to siegelfineart.com , when they go to the contact page, it will NOT WORK. I guess this is a technicality having to do with only sending email to the parent server? I don\\\\\\\\\\\\\\\'t know.
Do you know any solution to this? I would love it if I didn\\\\\\\\\\\\\\\'t miss out on customers who didn\\\\\\\\\\\\\\\'t bother typing www.
ALSO, FYI. I noticed that you updated your PHP code above by replacing that preg_match test. You left out a { at the end of that line, so it will always shoot back a syntax error. Just thought I\\\\\\\\\\\\\\\'d let you know.
Thanks again for everything. I doubt if I could have gotten this far on my contact form without your help.
Rob - June 5th, 2010 @ 9:36am reply
var url:String = "http://siegelfineart.com/sendMailTest.php";
Then it should work when the person goes to siegelfineart.com, however, it may not work if the person goes to www.siegelfineart.com
Elliott - June 13th, 2010 @ 8:38am reply
any idea what could be wrong anyone ! :D
Rob - June 13th, 2010 @ 8:44am reply
Elliott - June 13th, 2010 @ 12:31pm reply
what would i need to import also how do i link my boxes to the code you never mentioned instances names in the vid. thanks for responding so quickly been a great help i been stuck on doing a form for my site all day getting very frustrating when your a noob at programming :P
Help! :D
Rob - June 13th, 2010 @ 2:21pm reply
I'd like to suggest that you make a simple HTML form. Identical to the form that you want in flash, with the check boxes, radio buttons, etc. Just try getting this to work the way you want, including sending the email out.
You'll want to research POST, GET, and PHP forms... here's somewhere to start: http://www.w3schools.com/php/php_forms.asp
After you get the HTML form and processing script ready to go, it's only a matter of making the form in Flash and then making a URL request to the processing script.
You'll want to research linkage; how to connect flash components to AS3 objects.
Hope this helps & good luck,
Rob
Nathan - October 1st, 2010 @ 7:30pm reply
jack - September 25th, 2011 @ 12:47am reply
on (rollOver, dragOut) {
this.gotoAndPlay("over");
}
on (releaseOutside, rollOut)
{
this.gotoAndPlay("out");
}
on (release)
{
if (_root.pgap.pages1.page5.name1 == "Name" || _root.pgap.pages1.page5.company == "Company" || _root.pgap.pages1.page5.email == "E-mail" || _root.pgap.pages1.page5.number == "Phone" || _root.pgap.pages1.page5.message1 == "Message" || _root.pgap.pages1.page5.name1 == "" || _root.pgap.pages1.page5.company == "" || _root.pgap.pages1.page5.email == "" || _root.pgap.pages1.page5.number == "" || _root.pgap.pages1.page5.message1 == "")
{
_parent.gotoAndStop(3);
}
else
{
_parent.loadVariables("email.php","POST");
_parent.gotoAndStop(2);
} //end else if
}