1

I found a tutorial for send email using actionscripts 3 and php, i downloaded project files and copied mail.php to our VPS server xamp htdocs folder. Animate CC project run but nothing sent.

AS3:

submit_btn.addEventListener(MouseEvent.CLICK, sendMessage);
function sendMessage(e:MouseEvent):void
{
    var my_vars:URLVariables = new URLVariables();
    my_vars.senderName = name_txt.text;
    my_vars.senderEmail = email_txt.text;
    my_vars.senderMsg = message_txt.text;

    var my_url:URLRequest = new URLRequest("http://my-domain.com/mail.php");
    my_url.method = URLRequestMethod.POST;
    my_url.data = my_vars;

    var my_loader:URLLoader = new URLLoader();
    my_loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    my_loader.load(my_url);

    name_txt.text = "";
    email_txt.text = "";
    message_txt.text = "Message Sent";
} 

PHP:

<?php
$to = "email@yourDomain.com"; //Changed to my gmail
$subject = ($_POST['senderName']);
$message = ($_POST['senderMsg']);
$message .= "\n\n---------------------------\n";
$message .= "E-mail Sent From: " . $_POST['senderName'] . " <" . $_POST['senderEmail']  . ">\n";
$headers = "From: " . $_POST['senderName'] . " <" . $_POST['senderEmail'] . ">\n";
if(@mail($to, $subject, $message, $headers))
{
    echo "EMAIL SENT";
} 
else 
{
    echo "EMAIL NOT SENT";
}
?>

Animate CC Output:

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs. at Error$/throwError() at flash.net::URLVariables/decode() at flash.net::URLVariables() at flash.net::URLLoader/onComplete()

PHP Output:

Notice: Undefined index: senderName in /opt/lampp/htdocs/mail.php on line 3 Notice: Undefined index: senderMsg in /opt/lampp/htdocs/mail.php on line 4 Notice: Undefined index: senderName in /opt/lampp/htdocs/mail.php on line 6 Notice: Undefined index: senderEmail in /opt/lampp/htdocs/mail.php on line 6 Notice: Undefined index: senderName in /opt/lampp/htdocs/mail.php on line 7 Notice: Undefined index: senderEmail in /opt/lampp/htdocs/mail.php on line 7

EMAIL NOT SENT

sfxworks
  • 1,031
  • 8
  • 27
A.M
  • 345
  • 4
  • 13
  • I have no experinance with actionscript, but it looks like at least one of your problems is the post request keys dont seem to match up. In AS you set a propery called `senderName` but in php you attempt to read `name`. Perhaps changing your php post keys to match your AS property names would be a start: `$_POST['senderName']`,`$_POST['senderEmail']`,`$_POST['senderMsg']` – Steve Mar 01 '17 at 15:47
  • I changed code but nothing changed. – A.M Mar 01 '17 at 16:00
  • Comment the line with URLLoaderDataFormat.VARIABLES to avoid AS3 error. This line instructs that **answer** is a string of key1=value1&key2=value2 format, not the request. What for PHP, I'd start with print_r($_POST) and print_r($_GET) just to see what incoming data is. – Organis Mar 01 '17 at 16:21
  • You mean AS3 code is true, and can you as simple PHP for this? i have no experince with php. – A.M Mar 01 '17 at 16:46
  • will output the content of $_POST in readable format. – Organis Mar 01 '17 at 17:04
  • Output is: "Array()" no data show. – A.M Mar 01 '17 at 17:18
  • Then POST data is empty. Not long ago there was a topic about sending POST data: http://stackoverflow.com/a/41874224/4687633 – Organis Mar 01 '17 at 17:27
  • Change this line: var my_vars:URLVariables = new URLVariables(); to this one: var my_vars:Object = new Object(); – Mr. Phantom Mar 02 '17 at 15:42

2 Answers2

0

I have done this, it works great! IN FLASH Actionscript 3 define:

function f_sendMail() {
    var loader: URLLoader = new URLLoader;
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;

    var urlreq: URLRequest = new URLRequest("mysqlphp.php");
    urlreq.method = URLRequestMethod.POST;
    var urlvars: URLVariables = new URLVariables;
    //data to be sent to php
    //xdato1= name receibed php
    //wdato1= name in flash
    urlvars.xdato1 = wdato1;
    urlvars.xdato2 = wdato2;
    urlreq.data = urlvars;

    loader.addEventListener(Event.COMPLETE, f_completed);
    loader.addEventListener(IOErrorEvent.IO_ERROR, f_errorHandler);
    loader.addEventListener(IOErrorEvent.NETWORK_ERROR, f_errorHandler);
    loader.addEventListener(IOErrorEvent.VERIFY_ERROR, f_errorHandler);
    loader.addEventListener(IOErrorEvent.DISK_ERROR, f_errorHandler);
    try {
        loader.load(urlreq);
    } catch (error: Error) {
        sonidomal();
        errortxt.text = "A Error has occurred in send mail: " + error;
    }   
    function f_errorHandler(e: IOErrorEvent): void {
        errortxt.text = "errorHandler error=" + e.text;
    }   
    function f_completed(event: Event): void {
        if ((event.target.data.done == null) ||
           (event.target.data.done ==     undefined)) {
            errortxt.text = "Undefined";
        } else {
            if ((event.target.data.done.indexOf("Error") > -1) ||
               (event.target.data.done.indexOf("error") > -1)) {
                errortxt.text = event.target.data.done;
            } else {
                var textophp: String = event.target.data.done;
                //if you want to send data from php to flash
                //remember is by one string using echo
                //you have to separate it maybe ","
                trace ("mail ok");
            }
        }
    }
}

//----------------------------------------------
//In php
//receibe from flash
$flashdato1     =   $_POST['xdato1']; 
$flashdato2     =   $_POST['xdato2']; 
//do query to data base using $flashdato1 and $flashdato2 
//the result of query is in $rsSearchResults
if ($rsSearchResults){
    $rows = mysql_numrows($rsSearchResults);
    //put the information into $out 
    $body_content=$out; 
    //file name
    $file_name="datos/contestadop.csv"; 
    // Open the file in write mode, if file does not exist
    // then it will be created.
    $fp = fopen ($file_name, "w");
    // entering data to the file
    fwrite ($fp,$body_content); 
    fclose ($fp);// closing the file pointer
    //send mail
    //$mail_to and $mail_from must be set.
    $eol="\n"; 
    $mail_to = $fladdress; // Who is the E-Mail going to?
    $mail_from = 'mail.company.com.mx';
    // Where does the E-Mail appear to be from?  
    $host_url = $_SERVER['HTTP_HOST'];
    $message_subject = 'Monthly report'; 
    $headers = 'From: ' . $mail_from. "\r\n".'Reply-To:';
    $headers =.$mail_from."\r\n".'X-Mailer: PHP/'.phpversion();
    $headers .= "\n";    
    $message_body  = ' This is the information you requested';      
    $message_body .= $fltitrpte . "\r\n"; 
    $message_body .= ' data is separated by  "," , ' . "\r\n"; 
    $message_body .= ' Atte.'. "\r\n" ;
    $message_body .= ' Business department'. "\r\n" ;
    $message_body .= "\n\n";
    //File for Attachment 
    $f_name     =   $file_name;
    //use relative path OR ELSE big headaches.
    // $letter is my file for attaching.
    $handle     =   fopen($f_name, 'rb'); 
    $f_contents =   fread($handle, filesize($f_name)); 
    $f_type     =   filetype($f_name); 
    fclose($handle); 
    //Attachment 
    $mime_boundary   =  md5(time()); 
    $msg             = "";
    $msg            .= "--".$mime_boundary.$eol; 
    $msg            .= "Content-Type: application/txt; ".$eol;
    //sometimes i have to send MS Word, use 'msword' 
    //instead of 'pdf'
    $msg            .= "Content-Disposition: attachment;".$eol.$eol;
    // !! This line needs TWO end of lines !! IMPORTANT !!
    $msg            .= $body_content.$eol.$eol; 
    // # Finished 
    $msg            .= "--".$mime_boundary."--".$eol.$eol;
    // finish with two eol's for better security. see Injection.
    if (mail($mail_to, $message_subject, $message_body.$msg, 
       $headers)){
       echo "sent mail";
    }else{
        echo "Error envio de correo";
    }
}

Remember if your php is new you have to change sendmail file in php

mymiracl
  • 583
  • 1
  • 16
  • 24
0
//I think the error was you do not receive the answer from php correctly.
//In my example:
//In flash ".done" is the name field received from php:

function f_completed(event: Event): void { if ((event.target.data.done == null) ||...

//Then you have to finish php in this way:
//---
//Send data to flash
$done = "Send mail";
echo utf8_encode("done=".$done);
?>

//You do not have to write any echo during the php program
//Because it will send error to flash that is waiting for answer.
//Other tip.
//In php program at the beginning in "<?php" do not let any row empty after 
//or before because it causes error to flash