Home > OS >  Problems with Special Characters between Angular http post and PHP (and .net)
Problems with Special Characters between Angular http post and PHP (and .net)

Time:02-10

Email form on my site needs to be able to send an email via a server running with .net.

The code has worked beautifully, but the email server has been moved so that clients cannot directly access it. My primary server can talk to it and I am converting all of my client -> email server code to client - > PHP Server -> Email Server. (I have more than email running through it, and all of that is working now). I am struggling with special characters - my sample email:

Sample Email:

More special characters and multi-line

` ~ ! @ # $ % ^ & * ( ) _ - =

{ [ } ] \ | ; : ' " < , > . ? /

and finally a last line.

This Works (AngularJS):

$http({
                method:'POST',
                //url:  "assets/api/SendMessage.php",
                url:'https://myemailserver.com/mygroup/SendMessage',
                headers: {
                    'Content-Type': "application/json"
                },
                data:{
                    "From": dataObj['firstname-email']   ' '   dataObj['lastname-email'],
                    "SenderEmail": dataObj['senderemail-email'],
                    "Message": dataObj['body-email'],
                    "Subject": dataObj['subject-email']
                }
            }).then(function successCallback(response){
                console.log('Email Response, success!',response);
              
                $scope.emailSuccess = true;
            }, function errorCallBack(response){
                console.log('Email Error:',response);
                $scope.emailFail = true;
            })

But when I send it to my PHP Server (same code, different URL), which then uses curl to send the message, I get a failed message when there are any special characters including blank lines from the editor. (except , and .)

If the body of the message is a single line, then it works fine.

My PHP:

 $url = 'https://myemailserver.com/mygroup/SendMessage';   
$params = json_decode(file_get_contents('php://input'),true);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
    "Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = '{"From":"' . $params["From"] . '","SenderEmail":"' . $params["SenderEmail"] . '","Message":"' .$params["Message"] . '","Subject":"' . $params["Subject"] .'"}';

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
echo($resp);

I have tried several different options from the web:

  1. Remove all special characters from a string
  2. https://www.php.net/manual/en/function.addslashes.php
  3. https://www.w3schools.com/php/func_string_htmlspecialchars.asp

and either way I handle it, I get a failed response from the Email server.

I need to maintain special characters (including line breaks). Can anyone help me out with the how? Thanks

CodePudding user response:

It's almost certainly the way you're compiling your JSON string. Those things are very persnickety and if at all possible you should not create them manually. Rather create the object (or array) as intended and use the built-in tools to stringify.

Instead of $data = '{"From":"' . $params["From"] . '","SenderEmail":"' . $params["SenderEmail"] . '","Message":"' .$params["Message"] . '","Subject":"' . $params["Subject"] .'"}';

Try this:

$data = array(
   "From" => $params["From"],
   "SenderEmail" => $params["SenderEmail"],
   "Message" => $params["Message"], 
   "Subject" => $params["Subject"]);

$data = json_encode($data) ;

That is not only easier to read and maintain but automatically takes care of a host of issues that could be encountered in trying to escape content for transit.

  • Related