Android post request in php

I am trying to make PHP

sending a email

whenever I do post request

. If I load my site in web browser

, it does send mail. But whenever I post post request

to Android

(by calling the method postData

) nothing happens. Why doesn't it work?

public void postData(JSONObject object){
    //not using json object yet since i'm just testing...
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://test.com/email");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<>(2);
        nameValuePairs.add(new BasicNameValuePair("test1", "test1"));
        nameValuePairs.add(new BasicNameValuePair("test2", "test2"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler=new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost, responseHandler);

        //Just display the response back
        //displayToastMessage(responseBody);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

      

Here's my fancy php:

public function sendMail()
{
    $test = $_POST["test1"]
    $to = "marco.test@gmail.com";
    $subject = "HTML email";

    $message = "
        <html>
        <head>
        <title>HTML email</title>
        </head>
        <body>
        <p>This email contains HTML Tags!</p>
        <table>
        <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        </tr>
        <tr>
        <td>$test</td>
        <td>Doe</td>
        </tr>
        </table>
        </body>
        </html>
    ";

    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // More headers
    $headers .= 'From: <webmaster@example.com>' . "\r\n";
    $headers .= 'Cc: myboss@example.com' . "\r\n";

    mail($to,$subject,$message,$headers);
}

      

+3


source to share


3 answers


I think I found the problem and I think I can help others as well. I used Laravel to call a function, but Laravel didn't seem to like it. I downloaded a Chrome tool called Postman and submitted a manual submission request that made a mistake, perhaps to prevent tampering with fakes or some other hack. Now I just made it a split file and now it works!



+1


source


Try this code in PHP



$test=$_REQUEST['test1'];

      

+1


source


 public function sendMail($data)
    {
        $test = $data["test1"]
        $to = "marco.test@gmail.com";
        $subject = "HTML email";

        $message = "
            <html>
            <head>
            <title>HTML email</title>
            </head>
            <body>
            <p>This email contains HTML Tags!</p>
            <table>
            <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            </tr>
            <tr>
            <td>$test</td>
            <td>Doe</td>
            </tr>
            </table>
            </body>
            </html>
        ";

// Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
        $headers .= 'From: <webmaster@example.com>' . "\r\n";
        $headers .= 'Cc: myboss@example.com' . "\r\n";

        mail($to,$subject,$message,$headers);

    }
sendMail($_REQUEST);

      

Check this

+1


source







All Articles