0

I've found many samples on the internet regarding my question, but I simply want to send just a message/text and understand the connection between PHP and AS3.

2 Answers 2

3

Your php script will send mail in some way, and likely expect a few parameters sent through an Http Request, using either a POST or GET method. A typical php mail script looks like this...

<?php
    $to = $_POST["to"];
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    $from = "[email protected]";
    $headers = "From: $from";
    mail($to,$subject,$message,$headers);
    echo "Successfully sent";
?>

To call this script from ActionScript, you need to create a variables object.

var variables:URLVariables = new URLVariables();
variables.to = "whoever";
variables.message = "text";
variables.subject = "subject";

The variable names .to, .message, must match the php variables exactly.

now you can create your URLRequest Object, specifying the location of your script. Ensure the method is set to POST in this example. You add the variables above to the data object of the request.

var request:URLRequest = new URLRequest( "yourScript.php" );
request.method = URLRequestMethod.POST;
request.data = variables;

Then create a URLLoader and add an event listener. Do not pass the request created above to the constructor, but to the load method.

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete );
loader.load( request );

The handler could look something like this. This piece of code should trace out "Successfully sent".

private function onComplete( e:Event ) : void
{
    trace( URLLoader( e.target ).data.toString() );
}

Of course you could add error handling listeners as well, just in case something went wrong, but if you have control over the php script and the action script, you shouldn't have any problems.... usually

Sign up to request clarification or add additional context in comments.

Comments

0

To put Nicholas's answer a little more simply, the mail() command is just this

<?php
mail(
    "[email protected]",
    "This is the title, or subject",
    "This is the body of the message",
    "From: Emailer <[email protected]>"
);
?>

In some instances you may have to configure your PHP.ini file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.