0

I'm using flash CS6 and flash player 11.4

Here is the example sending email from Flash using PHP

as3 code:

var php_file = "simple_email.php";
var message_text = "Hello Im mesage from flash.";

function sendEmail():void
{
   var myData:URLVariables = new URLVariables();
   myData.msg = message_text;

   var myRequest:URLRequest = new URLRequest(php_file);
   myRequest.data = myData;
   myRequest.method = URLRequestMethod.POST;

    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    loader.addEventListener(Event.COMPLETE, completeHandler);
    try {

        loader.load(myRequest);

    } catch (error:Error) {

        trace("Unable to load URL");
    }

    function completeHandler(e:Event):void {

      trace("Data Content:"+e.target.data)
      trace("Response:"+e.target.data.success);
    };
};

sendEmail();

php code:

<?php
    $msg = $_POST["msg"];
    $to = "webhosting4@outlook.com";
    $subject="Message from php";
    $success = mail($to,$subject,$msg,"Content-Type: text/plain; charset=utf-8");
    echo "temp=1&success=".$success;
?>

all is look simple, but not working

1) the Response: should be true or false and is:".$success; ?> (bad parsing?)

2) e.target.data looks strange:

Data Content:success=%22%20%24success%3B%0D%0A%3F%3E&%3C%3Fphp%0D%0A%09%24msg%20=%20var%5Fexport%28%24%5FPOST%2C%20true%29%3B%0D%0A%09%24to%20%3D%20%22baca%2Erene%40gmail%2Ecom%22%3B%0D%0A%09%24subject%3D%22Message%20from%20php%22%3B%0D%0A%09%24success%20%3D%20mail%28%24to%2C%24subject%2C%24msg%2C%22Content%2DType%3A%20text%2Fplain%3B%20charset%3Dutf%2D8%22%29%3B%0D%0A%09echo%20%22temp%3D1

3) After executing code I not received any email....something must be wrong?

CyanAngel
  • 1,240
  • 1
  • 14
  • 28
Orien
  • 47
  • 2
  • 7
  • You have a problem in your web server (if you have one of course) because php is not executed. You should know that you can not run a php script without a web server. – akmozo Jan 06 '15 at 14:02

1 Answers1

0

In order:

  1. In PHP Booleans do not echo out "true" or "false" like they do in actionscript-3s trace(). You need to use some form of if statement in PHP to echo an appropriate string rather echoing the variable directly.

  2. e.target.data is returning an utf escaped string version of your PHP code, this is usually a sign that php isn't executing and the file is just being read as a string, you should check your server is configured correctly and check the PHP error log (and alter your php config to write all errors to the log)

  3. Yes, something is wrong, check your PHP configs and error logs, quite likely and SMTP config error.

You can also use error_get_last(); in php to pull the last error thrown by php.

Community
  • 1
  • 1
CyanAngel
  • 1,240
  • 1
  • 14
  • 28
  • I think that @Orien is trying to execute a local php script without even a web server. – akmozo Jan 06 '15 at 14:13
  • Not having a webserver might be the root cause, but it doesn't provide an explanation of point one (which wouldn't work even with a webserver) or point two (which is confusion over utf character escaping) – CyanAngel Jan 06 '15 at 14:24
  • In reality, his php code is fine just because there is no web server to execute it, flash will load it as any text file. Echo PHP boolean gives 1 or 0. – akmozo Jan 06 '15 at 14:38
  • That seems to contradict the Q&A I linked in point one, do you have a source for that? (I don't have a server to hand to test it) although on second look at e.target.data, it is all the php code not a section as I first thought, which makes your point more likely, editing my answer now – CyanAngel Jan 06 '15 at 14:40
  • I didn't mean simply `echo $var; `, of course if `$var` is `false` we will get nothing, I meant using the appropriate manner like `echo intval($var);`, for example, which gives 1 or 0 for a boolean. – akmozo Jan 06 '15 at 15:12
  • @Orien I don't understand your last note, what do you mean by "but this return me intval($var); without execute" ? – akmozo Jan 06 '15 at 16:29
  • thanks for you answers guys, [akmozo] you are right, I'm try to execute this app localy. return value 1 or 0 should be enough, but this return me intval($var); without result. maybe it is because as [CyanAngel] says that php isn't executing. That mean I cant use error_get_last(), error_log("You messed up!", 3, "/php-error.log"); because php is not executing , only retunr his content as string.... – Orien Jan 06 '15 at 16:31
  • when i add in php this line echo intval($success) I got Response:undefined; – Orien Jan 06 '15 at 16:36
  • when i add in php this line echo "temp=1&success=".intval($success); I got Response:".intval($success); ?> – Orien Jan 06 '15 at 16:37
  • @Orien First, you have to install a web server, you can not execute a php script directly, flash will consider it as any text file, that's why you will get always the script code in your response. – akmozo Jan 06 '15 at 16:37
  • @Akmozo that should be the reason, I will do as you say. Many thanks :-) – Orien Jan 06 '15 at 16:42