Sunday, July 17, 2011

AS3 Code: E-mail Form [PHP] Another Example

I've discovered another way to send data from Flash to an e-mail recipient (see previous post). The way described below uses code similar to writing to a text file.



//AS3 code in Flash:
var variables:URLVariables;var fLoader:URLLoader;var fData:URLRequest;
 
function onSubmit(e:Event):void{
    variables = new URLVariables();
    fData = new URLRequest("email.php");
    fData.method = URLRequestMethod.POST;
    fData.data = variables;

    fLoader = new URLLoader();
    fLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    fLoader.addEventListener(Event.COMPLETE, sendComplete);
    variables.Name = name_txt.text;
    variables.Email = email_txt.
text;
    variables.Phone = phone_txt.
text;
    variables.Comments = comments_txt.
text;
    fLoader.load(fData);
}



//PHP file called "email.php":
< ?php 
//define the receiver of the email 
$to = 'email@email.com'; 
//define the subject of the email 
$subject = 'The Subject'; 
//define the message to be sent
$message = $_POST['Name']." filled out the form.\r\n"."\r\n".$_POST['Name']."\r\n".$_POST['Email']."\r\n".$_POST['Phone']."\r\n".$_POST['Comments'];
//define the headers we want passed.
$headers = "From: email@email.com\r\nReply-To: email@email.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>



0 comments:

Post a Comment