4

I have the following mail.php file :

<?php
$name = $_POST['name'];
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = "From: ".$name."\r\n";
$message .= $_POST['message'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?> 

I have following layout to send mail from my app: enter image description here

I want to send mail to static mail eg: mymail@mail.com after clicking send button.
How can i send mail by calling above php file.
I heard about post method to call above php file But dont have any idea about it.
Help Please !

captaindroid
  • 2,868
  • 6
  • 31
  • 45
  • you'll have to send a request to your server through HTTP/s. Similar question: http://stackoverflow.com/questions/2938502/sending-post-data-in-android – blejzz Mar 29 '12 at 11:52
  • [Google is your friend](https://www.google.pt/search?q=java+http+post). http://www.exampledepot.com/egs/java.net/post.html In this example `key1`, `key2`, `keyn` would map to your PHP `$_POST` fields. – Telmo Marques Mar 29 '12 at 11:55

2 Answers2

5

Here it is:

    public static void sendData(String name, String to, String from, String subject, String message)
    {
        String content = "";

        try
        {               
            /* Sends data through a HTTP POST request */
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://your.website.com");
            List <NameValuePair> params = new ArrayList <NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            params.add(new BasicNameValuePair("to", to));
            params.add(new BasicNameValuePair("from", from));
            params.add(new BasicNameValuePair("subject", subject));
            params.add(new BasicNameValuePair("message", message));
            httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

            /* Reads the server response */
            HttpResponse response = httpClient.execute(httpPost);
            InputStream in = response.getEntity().getContent();

            StringBuffer sb = new StringBuffer();
            int chr;
            while ((chr = in.read()) != -1)
            {
                sb.append((char) chr);
            }
            content = sb.toString();
            in.close();

            /* If there is a response, display it */
            if (!content.equals(""))
            {
                Log.i("HTTP Response", content);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
Manitoba
  • 8,522
  • 11
  • 60
  • 122
0

Either you have to load web view or write a web service and pass the parameters as JSON.

GavinCattell
  • 3,863
  • 20
  • 22
Arun Killu
  • 13,581
  • 5
  • 34
  • 61