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,$message,$headers);
?>