Reputation: 2659
I have the following function in my Android app:
void sendEmail(String PHPfileUurl, String receiverEmail, String fromEmail) {
ParseUser currentUser = ParseUser.getCurrentUser();
StringBuilder messageBuilder = new StringBuilder();
for (int i=0; i<productsOrdered.size(); i++){
messageBuilder.append(productsOrdered.get(i)).append("\n");
}
String mess = messageBuilder.toString();
String parameters = "name=" + currentUser.getString(Configurations.USER_FULLNAME) +
"&fromEmail=" + fromEmail +
"&receiverEmail=" + receiverEmail +
"&messageBody=" + mess +
"&storeName=" + Configurations.MERCHANT_NAME +
"&shippingAddress=" + currentUser.getString(Configurations.USER_SHIPPING_ADDRESS);
String strURL = PHPfileUurl + parameters;
strURL = strURL.replace(" ", "%20");
strURL = strURL.replace("\n", "%20");
Log.i(Configurations.TAG, "PHP STRING URL: " + strURL);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
URL url;
url = new URL(strURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(20000);
conn.setReadTimeout(20000);
conn.setDoInput(true);
conn.setDoOutput(true);
if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){
InputStream is = conn.getInputStream();
Log.i(Configurations.TAG, "EMAIL RESPONSE: " + conn.getResponseMessage());
} else {
InputStream err = conn.getErrorStream();
Log.i(Configurations.TAG, "ERROR ON EMAIL: " + err);
}
} catch (IOException e) {e.printStackTrace(); }
}
When I call that function the Logcat prints out this message:
I/log-: PHP STRING URL: http://example.com/myapp/email-admin.php?name=Mark%20Doe&[email protected]&[email protected]&messageBody=PRODUCT%20ID:%20Q3nQgZdlFG%20---%20PRODUCT:%20Nike%20Sport%20Shoes%20black%20%20---%20QUANTITY:%201%20---%20SIZE:%20L%20&storeName=Z%20Store%20Inc.&shippingAddress=John%20Doe,%20121%20Church%20Avenue,%20ASD123,%20London,%20UK
I/log-: EMAIL RESPONSE: OK
So I assume everything is fine since the RESPONSE = OK. But it's not, because I will not receive any email at [email protected] (there is another email address, I've posted a fake one just as an example, the Logcat prints out my real email address as receiverEmail
).
Here's my mail.php
file:
// POST Variables
$name = $_POST['name'];
$fromEmail = $_POST['fromEmail'];
$receiverEmail = $_POST['receiverEmail'];
$messageBody = $_POST['messageBody'];
$storeName = $_POST['storeName'];
$shippingAddress = $_POST['shippingAddress'];
$headers = 'From: ' .$fromEmail;
// SUBJECT
$subject = "New order from " .$name. " on '" .$storeName. "'";
// COMPOSE MESSAGE
$message =
"ORDER DETAILS:\n".
$messageBody.
"\n\nName: " .$name.
"\nUser Email: " .$fromEmail.
"\nShipping Address: " .$shippingAddress
;
/* Finally send email */
mail($receiverEmail,
$subject,
$message,
$headers
);
/* Result */
echo "Email Sent to: " .$receiverEmail. "\n Message: " .$message;
Does my code have something wrong? is there another way to call a mail.php file from my own server? I've also tried this question, but I cannot import the DefaultHttpClient
class in my project.
Upvotes: 1
Views: 91
Reputation: 519
it's would be easier if you change the $_POST to $_GET but the problem in the $_GET method if the message have (&something=) inside it you will receive only half the message as the &something= would be set to an other $_GET , Also you might get some problems if the message is too long ,
so if you want to use the $_POST method instead of the $_GET
you need to change your java code , make sure to import Map and then change it to this
void sendEmail(String PHPfileUurl, String receiverEmail, String fromEmail) {
ParseUser currentUser = ParseUser.getCurrentUser();
StringBuilder messageBuilder = new StringBuilder();
for (int i=0; i<productsOrdered.size(); i++){
messageBuilder.append(productsOrdered.get(i)).append("\n");
}
String mess = messageBuilder.toString();
Map<String,Object> params = new LinkedHashMap<>();
params.put("name", currentUser.getString(Configurations.USER_FULLNAME));
params.put("fromEmail", fromEmail);
params.put("receiverEmail", receiverEmail);
params.put("messageBody", mess);
params.put("storeName", Configurations.MERCHANT_NAME);
params.put("shippingAddress", currentUser.getString(Configurations.USER_SHIPPING_ADDRESS);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
String strURL = PHPfileUurl;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
URL url;
url = new URL(strURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(20000);
conn.setReadTimeout(20000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){
InputStream is = conn.getInputStream();
Log.i(Configurations.TAG, "EMAIL RESPONSE: " + conn.getResponseMessage());
} else {
InputStream err = conn.getErrorStream();
Log.i(Configurations.TAG, "ERROR ON EMAIL: " + err);
}
} catch (IOException e) {e.printStackTrace(); }
}
Upvotes: 1
Reputation: 519
Use $_GET instead of $_POST ,
change all variable from
$name = $_POST['name'];
to
$name = $_GET['name'];
Upvotes: 1