Simple example – Flash (AS3) and FormMail.cgi
I’ve just created a little sample for a friend. He is a designer and “just needed something” that would allow him to capture the data that users enter. FormMail.cgi fits the bill – no need to touch any server files or databases, just point your Flash to a FormMail script that is already installed on your server and off you go – you are sending emails from Flash. It feels bit 90-ies, but it works.
What will you need:
- Flash CS3 or newer (download free 30 days trial from Adobe)
- URL to FormMail.cgi. Many hosting providers have installed FormMail.cgi for you – you will definitely have it if you have cPanel, look under Scripts to find the right path to yours. Otherwise ask your hosting provider or pinch somebody else’s FormMail (not recommended
).
Here are instructions that any Flash designer should be able to follow:
- Create a AS3 Flash document.
- Create your text inputs. My code is using 3 Text Inputs named: messageTI, nameTI, emailTI. You can create more, or rename them, but then you will have to add/modify the code a little bit.
- Create a button and name it sendB
- Create a dynamic text and name it statusTF
- Create an Actions layer and in the first frame put the following code, than modify the FormMail URL and recipient’s email:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import flash.events.MouseEvent; import flash.profiler.showRedrawRegions; import flash.net.URLRequest; import flash.net.URLRequestMethod; sendB.addEventListener(MouseEvent.CLICK, sendData); function sendData(e:MouseEvent):void { if (messageTI.text.length > 3 && nameTI.text.length > 3 && emailTI.text.indexOf("@") > -1) { //very, very basic data validation (as this is not a data validation tutorial) var r:URLRequest = new URLRequest("http://YOUR-URL-TO-FORM-MAIL/FormMail.cgi"); //modify this URL var v:URLVariables = new URLVariables(); v.recipient = "YOUR-EMAIL@DOMAIN.com"; //put your email here v.subject = "My FormMail test"; //this will be the subject of the email v.email = emailTI.text; v.realname = nameTI.text; //user's name will be appended to his/hers email to form a From field of the email. v.theMessage = messageTI.text; //theMessage is a custom key. If you need more Input fields, just make up more custom fields like this one r.data = v; r.method = URLRequestMethod.POST; var loader:URLLoader = new URLLoader(); loader.addEventListener(Event.COMPLETE, completeHandler); try { loader.load(r); } catch (error:Error) { statusTF.text = "Unable to send data."; //we could definitely do more in terms of error handling, however this is not a error handling tutorial } } else { statusTF.text = "Please enter all required information."; } } function completeHandler(event:Event):void { statusTF.text = "Data submitted."; var loader:URLLoader = URLLoader(event.target); trace("completeHandler: " + loader.data); } |
Notes:
- You can place your components (Button, TextInputs & Dynamic Text) anywhere you like on a page, style them anyhow you like, but make sure they are in the first frame and have the correct names (case sensitive).
- If email is getting send but you are not receiving any – check your spam folder.
- You are too lazy to copy/paste? I hear you. Here is the sample flash file.
| Print article | This entry was posted by Stepan on May 29, 2010 at 12:03 pm, and is filed under How-tos and Tutorials. Follow any responses to this post through RSS 2.0. You can skip to the end and leave a response. Pinging is currently not allowed. |

about 1 year ago
I’m receiving an error indicating I’ve failed to specify the POST method.
Below is the Network Monitor Error:
Error: GET request
The HTML form fails to specify the POST method, so it would not be correct for this script to
take any action in response to your request.
If you are attempting to configure this form to run with FormMail, you need to set the request
method to POST in the opening form tag, like this:
href=”http://nms-cgi.sourceforge.net/”>FormMail
© 2001-2003 London Perl Mongers
about 1 year ago
Strange. Are you sure did not forget this line?
about 1 year ago
I finally found that I had a for the service that I had coded in an earlier attempt. This was interfering with this new approach. I deleted the and it works like a charm now!
Thanks
about 1 year ago
Tiger mac osx 10.4 server
creating a page with flash cs3
with a contact form
Need to place a or whatever to make
a cgi script to work.
First do not know anything but willing to learn as I go..
Will look for the cgi and where does it go on the server.
Like what you have written up above but I would think that
mac osx server has some .cgi excutables or configuration ready to go
and just place the location of it.
Pardon my ignorance. Thanks for people like you.
Best regards,
Manu