0

I want to send an email on button click on android app, executing a php file on the server (with mail function)

I guess I am able to connect to the server and open the file, but it does not execute For some reason it does not work. All it displays in the android console is the following I/System.out: (HTTPLog)-Static: isSBSettingEnabled false

Below is the code

Any help will be appreceated

 public static void sendData() {


    try {
        URL url = new URL("http://myurl/mail.php");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);


        HashMap<String, String> params = new HashMap<String, String>();
        params.put("name", name);
        params.put("to", mail);
        params.put("from", from);
        params.put("subject", subject);
        params.put("message", message);

       OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getQuery(params));
        writer.flush();
        writer.close();
        os.close();
        conn.connect();


    }
    catch (Exception e) {
        e.printStackTrace();
    }

}

public static String getQuery(HashMap<String, String> params) throws UnsupportedEncodingException{
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for(Map.Entry<String, String> entry : params.entrySet()){
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}

my php file 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,$m‌​essage,$headers); 
?>
denis
  • 1
  • 4
  • where's the php for this? you did tag as such. Not to mention phpmailer. – Funk Forty Niner Aug 16 '16 at 14:03
  • well pho is a simple one – denis Aug 16 '16 at 14:04
  • please don't dump code in comments. Edit your question http://stackoverflow.com/posts/38976965/edit to contain it. Plus, `mail()` is not phpmailer; two different animals altogether ;-) – Funk Forty Niner Aug 16 '16 at 14:05
  • sorry, new to this...ok..i use php mail, any idea why it does not work? – denis Aug 16 '16 at 14:06
  • If it's a php issue, use error reporting. Change your `mail($to,$subject,$m‌​essage,$headers);` to `if(mail($to,$subject,$m‌​essage,$headers)){ echo "Mail sent.";} else{ echo "Error";}` - If it's an android issue, I am not the guy for this. – Funk Forty Niner Aug 16 '16 at 14:09
  • when i execute the file from the server it works fine, i guess its an android issue – denis Aug 16 '16 at 14:13
  • Where are the variables `name`, `mail`, etc. coming from? My guess is that those are instance members while your `sendData()` method is a class member. – simon Aug 16 '16 at 14:19
  • well actually the variable names just for the try, are hardcoded like this params.put("name", "justaname"); – denis Aug 16 '16 at 14:22
  • John Conde, its not a php question its an android question. php works just fine – denis Aug 16 '16 at 15:45

0 Answers0