How to send mail with PHP in ActionScript 3?
I've found many samples on the internet regarding my question, but I simply want to send just a message/t开发者_StackOverflow社区ext and understand the connection between PHP and AS3.
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 = "youre@email.com";
$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
To put Nicholas's answer a little more simply, the mail() command is just this
<?php
mail(
"email@website.com",
"This is the title, or subject",
"This is the body of the message",
"From: Emailer <email@website.com>"
);
?>
In some instances you may have to configure your PHP.ini file.
精彩评论