As3 php send mail to multiple users

Here is an example of how you can send an email to multiple users using ActionScript 3 (AS3) and PHP:

AS3 Code

First, you need to create a PHP script that will handle the email sending. We'll get to that in a minute. For now, let's assume you have a PHP script called send_email.php that takes two parameters: to and message.

In your AS3 code, you can use the URLLoader class to send a request to the PHP script and pass the to and message parameters. Here's an example:

import flash.net.URLLoader;
import flash.net.URLRequest;

// assume you have a list of email addresses in an array
var toAddresses:Array = ["[email protected]", "[email protected]", "[email protected]"];

// create a URL request
var request:URLRequest = new URLRequest("send_email.php");
request.method = URLRequestMethod.POST;

// create a URL variable to pass the to addresses
var toVariable:URLVariable = new URLVariable();
toVariable.name = "to";
toVariable.value = toAddresses.join(",");

// create a URL variable to pass the message
var messageVariable:URLVariable = new URLVariable();
messageVariable.name = "message";
messageVariable.value = "Hello, this is a test email!";

// add the variables to the request
request.data = toVariable;
request.data = messageVariable;

// create a URL loader
var loader:URLLoader = new URLLoader();
loader.load(request);

// listen for the completion event
loader.addEventListener(Event.COMPLETE, onEmailSent);

PHP Code

Now, let's create the PHP script that will handle the email sending. This script will take the to and message parameters and send an email to each of the recipients using the PHP mail() function.

Here's an example:

<?php

// get the to addresses and message from the request
$to = $_POST['to'];
$message = $_POST['message'];

// split the to addresses into an array
$to_addresses = explode(",", $to);

// send an email to each recipient
foreach ($to_addresses as $address) {
    $headers = "From: [email protected]\r\n";
    $headers.= "Reply-To: [email protected]\r\n";
    $headers.= "MIME-Version: 1.0\r\n";
    $headers.= "Content-Type: text/plain; charset=UTF-8\r\n";

    mail($address, "Test Email", $message, $headers);
}

// output a success message
echo "Email sent successfully!";
?>

Putting it all together

To use this code, you'll need to create a PHP script called send_email.php and upload it to your server. Then, in your AS3 code, you can use the URLLoader class to send a request to the PHP script and pass the to and message parameters.

For example, if you have a button in your AS3 application that you want to use to send the email, you can add an event listener to the button's click event and use the code above to send the email.