2

I have a post function to insert info about the process on database and send email to one recipient.

$app->post('/sendemail', function ($request, $response) {
    //Save info process
    $message =  saveDataBaseInfo();

    if ($message == "OK") {         

        // Send email
        $res = sendEmail("Some text", "<p>Some text</p>", "Some subject", "email@email.com");

        if ($res == "Fail") {
            return $res;
        }
    }     
});

public function sendEmail($body, $bodyHtml, $subject, $emailTo) {

    $mail = new PHPMailer;

    $mail->SingleTo = true;
    $mail->isSMTP();
    $mail->Host = 'myHost';
    $mail->SMTPAuth = true;
    $mail->Username = 'theuser';
    $mail->Password = '******';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;  
    $mail->setFrom("emailfrom@emailfrom.com", "", false);
    $mail->addAddress($emailTo);
    $mail->isHTML(true);

    $mail->Subject = $subject;
    $mail->Body = $bodyHtml;
    $mail->AltBody = $body;
    $send = $mail->send();

    if (!$send) {
        return "Success"
    } else {
        return "Fail";
    }
}

And I call this function on my Android app like the code below

// Click event function
private void sendEmailClick() { 

    HashMap<String, String> parms = new HashMap<>();
    // parms like emailTo for example

    TaskServerAsync taskServerAsync = new TaskServerAsync(this, parms);
    taskServerAsync.execute(R.string.urlSendEmail);
}

public class TaskServerAsync extends AsyncTask<Integer, String, JSONObject> implements Response.ErrorListener {   
    private Map<String, String> parm;
    private Context context;

    public TaskServerAsync(Context context ,Map<String, String> parms) {
        this.parm = parm;
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        //Show progressBar
        progressBar.show();
    }

    @Override
    protected JSONObject doInBackground(Integer... values) {
        RequestFuture<JSONObject> future = RequestFuture.newFuture();

        // Class send request server with volley
        ServerConn.send(context, values[0], parms, future, this);

        JSONObject json = null;
        try {
            // Get future response
            json = future.get(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

        return json;

    }

    @Override
    protected void onProgressUpdate(String... values) {

    }

    @Override
    protected void onPostExecute(JSONObject response) {        
        dismissProgressBar();
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        // Show error server and dismiss ProgressBar
        dismissProgressBar();
    }

    private void dismissProgressBar() {
        if (progressBar != null)
            progressBar.dismiss();
    }
}

But the post function fired twice. What's wrong? Need I configure something on apache? Someone can helps me?

alexw
  • 8,468
  • 6
  • 54
  • 86
Vinicius Maciel
  • 101
  • 2
  • 12
  • How are you calling this function? For example, are you using AJAX to post? – Mikey May 23 '16 at 17:14
  • I have an android app, and I'm calling this function with Volley Api – Vinicius Maciel May 23 '16 at 17:16
  • This makes your problem more complicated then. You should update your question with the Java code that you use to post. It could be a problem with your Java code. Without using your Android app, if you try and post to this route, does it also send twice? – Mikey May 23 '16 at 17:24
  • 2
    I'd bet you're calling the function twice. Stick a timestamp in the subject: `$mail->Subject .= md5(microtime(true));`. Check your logs too. – Synchro May 23 '16 at 17:25
  • On Android project I call server with Volley within AsyncTask process to run in background – Vinicius Maciel May 23 '16 at 17:31
  • I updated how I call the server function on android project – Vinicius Maciel May 23 '16 at 17:43
  • Are you sure that `sendEmailClick` on the Android side isn't fired twice? Once for `buttonPressed` and once of `buttonReleased`? Just guessing, because the code for that is missing. – puelo May 23 '16 at 18:01
  • I did a test. When I commented the send email code block, the function post was called once. So I think the problem is the phpmailer. Something is missing I think. – Vinicius Maciel May 23 '16 at 18:58
  • 1
    Which is the "send email code block"? Uncomment the code and temporarily rename `$app->post('/sendemail', ...)` to `$app->get('/sendemail', ...)` and directly access this route via your browser. Does it send the email twice? If so, then you are probably on the right track. If not, I am guessing this has something to do with Android/Volley. – Mikey May 23 '16 at 22:13
  • @Mikey you're right, my Volley calls produce that problem. I changed post to get and I tested on browser, as you say before. One email was sent. Now, how can I fix that problem? – Vinicius Maciel May 24 '16 at 00:34
  • 1
    I found the solution. Thanks all to helps me. @Mikey thank you, you saved my day. Below the solution for volley's bug http://stackoverflow.com/a/27873079/2154577 – Vinicius Maciel May 24 '16 at 01:30

1 Answers1

0

As noted, the problem is that the Android app is sending calling the API twice.

Community
  • 1
  • 1
Rob Allen
  • 12,643
  • 1
  • 40
  • 49