Avg. Rating 4.1

Problem

How to create an email form in AS3 without any redirection to a mail application, such as Mail (mac) or MS Outlook... Have the form and the confirmation all in the same flash.

Solution

First you need to be aware of your server and back-end script capabilities. I'm using my host which runs PHP (which is probably the only language I am comfortable writing in and there are tons of resources online) Then, I am considering factors such as: People may send emails not using real emails so you will possibly need to add validation. To get started: We are going to use AS3 but we are also going to use PHP as back-end. Everything else is done in one swf.

Detailed explanation

I made two movieclips that I used as buttons: sendbtn and resetbtn .

I created input fields called: yourName , fromEmail, yourSubject and YourMsg.

They post the variables: name, from, subject and msg to the PHP.

/*************************************    
Buttons       
**************************************/ 

sendbtn.buttonMode = true;

sendbtn.addEventListener(MouseEvent.CLICK, submit);

resetbtn.buttonMode = true;

resetbtn.addEventListener(MouseEvent.CLICK, reset); 

/*************************************        
Variables needed        
**************************************/  

 var timer:Timer; var varLoad:URLLoader = new URLLoader;

 var urlRequest:URLRequest = new URLRequest( "mail.php" );

 urlRequest.method = URLRequestMethod.POST;

/*************************************
Functions
**************************************/ 

 function init():void{

 //Set all fields to empty

 yourName.text = "";

 fromEmail.text = "";

 yourSubject.text = "";

 YourMsg.text = "";

 }

 function submit(e:MouseEvent):void{

 //Check to see if any of the fields are empty

 if( yourName.text == "" || fromEmail.text == "" ||
yourSubject.text == "" ||YourMsg.text == "" ) { valid.text = "  
All fields need to be filled.";

 }

 //Check if you're using a valid email address

 else if( !checkEmail(fromEmail.text) )

 { valid.text = "
Enter a valid email address"; } 


else { valid.text = "
Sending over the internet...";

 var emailData:String = "
name=" + yourName.text + "&
from=" + fromEmail.text + "&
subject=" + yourSubject.text + "&
msg=" + YourMsg.text;

var urlVars:URLVariables = new URLVariables(emailData);
urlVars.dataFormat = URLLoaderDataFormat.TEXT;
urlRequest.data = urlVars; varLoad.load( urlRequest );
varLoad.addEventListener(Event.COMPLETE, thankYou );
 } 
}
function reset(e:MouseEvent):void{
init(); //call the initial clear function
 }
function checkEmail(s:String):Boolean {

 //This tests for correct email address

var p:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/; var
r:Object = p.exec(s);
if( r == null ) {
return false;

}

return true;

}

function thankYou(e:Event):void { 
var loader:URLLoader =
URLLoader(e.target); 
var sent = new
URLVariables(loader.data).sentStatus; if( sent == "yes" )
{
valid.text = "
Thanks for your email!"; timer = new Timer(500);
timer.addEventListener(TimerEvent.TIMER, msgSent);
timer.start();
}
else {
valid.text = "
Oh no! Something is wrong! Try again..."; }

}

function msgSent(te:TimerEvent):void {
if( timer.currentCount >= 10 ) { init();
timer.removeEventListener(TimerEvent.TIMER, msgSent);

}

}

-------------

PHP file called mail.php with the code:

<?php $yourName = $_POST['name']; // the variable needsto match
your Actionscript

$fromEmail = $_POST['from']; // the variable needs to match your
Actionscript

$yourSubject = $_POST['subject']; // the variable needs to match
your Actionscript

$YourMsg = $_POST['msg']; // the variable needs to match your
Actionscript

if( $yourName == true ) { $sender = $fromEmail; $yourEmail ="
yourEmailAddress@yourdomain.com"; // This will be
your email address so please change this

$ipAddress = $_SERVER['REMOTE_ADDR']; // This gets the user's ip
Address

$emailMsg = "Name: $yourName sent this from IP:
$ipAddress\n\nReturn Email: $sender
\n\nSubject:$yourSubject\n\nMessage:\n\n$YourMsg \n\nThis email was
sent using
a form on your site";$return = "From: $sender\r\n" .
"Reply-To:$sender \r\n" ."X-Mailer: PHP/" . phpversion();

if( mail( $yourEmail, "$yourSubject", $emailMsg, $return
))

{ 
echo "sentStatus=yes"; }

else { echo "sentStatus=no"; } }
?> 



  emailer.zip *remember to change your email address in the PHP where it says

yourEmailAddress@yourdomain.com

Hope this helps!

Jerlyn Jerlyn Thomas

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes