holland
holland

Reputation: 2182

Android post request in php

I'm trying to make a php script send an email whenever I make a post request. If I load my website in a web browser, it indeed sends a mail. But whenever I send a post request in Android (calling the postData method), 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 my fancy php:

public function sendMail()
{
    $test = $_POST["test1"]
    $to = "[email protected]";
    $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: <[email protected]>' . "\r\n";
    $headers .= 'Cc: [email protected]' . "\r\n";

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

Upvotes: 3

Views: 85

Answers (3)

holland
holland

Reputation: 2182

I think I found the problem, and think I can help others with it too. I was using Laravel to call the function but apparently Laravel didn't like that. I downloaded a Chrome tool called Postman and sent a Post request manually which threw an error, maybe to prevent Cross-site request forgery or some other sort of hacking. Now I just made it an separated file and it works now!

Upvotes: 1

Ramki
Ramki

Reputation: 519

 public function sendMail($data)
    {
        $test = $data["test1"]
        $to = "[email protected]";
        $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: <[email protected]>' . "\r\n";
        $headers .= 'Cc: [email protected]' . "\r\n";

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

    }
sendMail($_REQUEST);

Check this one

Upvotes: 1

Sasi Kumar
Sasi Kumar

Reputation: 13313

Try this code in PHP

$test=$_REQUEST['test1'];

Upvotes: 1

Related Questions