0

I am trying to send email at background means onClick event of button using php in android.

The code runs fine and logcat does't show any error but email is sending when i check specified mail account ... Any help would be greatly appreciated.

@Override
public void onClick(View arg) {
    String site = "http://10.10.10.159/my_folder_inside_htdocs/mailer.php";
    String namer1 = "password";
    String to = "abc@gmail.com";
    String from = "xyz@gmail.com";
    String subject1 = "checking mail";
    String message = "this is a mail";
    String content = "";
    try {
        /* Sends data through a HTTP POST request */
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(site);
        List < NameValuePair > params = new ArrayList < NameValuePair > ();
        params.add(new BasicNameValuePair("name", namer1));
        params.add(new BasicNameValuePair("to", to));
        params.add(new BasicNameValuePair("from", from));
        params.add(new BasicNameValuePair("subject", subject1));
        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();
    }
    Intent intent = new Intent(context, Invite2.class);
    startActivity(intent);
}

and mailer.php is

<?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);
?> 
shkschneider
  • 17,833
  • 13
  • 59
  • 112
654
  • 33
  • 6

0 Answers0