phpandroidandroid-volleyphpmailerway2sms

php sending sms and email 3 times (Using Android as frontend and sending request to server using volley)


I'm trying to send sms through way2sms api and mail through phpmailer api but the problem is I'm receiving 3 sms and 3 mails when i run the php.

 <?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $status = $_POST['status'];
    $uname  = $_POST['uname'];
    $dept   = $_POST['dept'];
    $ename  = $_POST['ename'];

    require_once('dbConnect.php');

    $sql      = "SELECT name,email FROM user WHERE mobile='" . $uname . "'";
    $check    = mysqli_fetch_row(mysqli_query($con, $sql));
    $username = $check[0];
    $email    = $check[1];

    require_once 'smsapi/way2sms-api.php';
    sendWay2SMS('username', 'password', $uname,' Message');

    require_once 'mailapi/PHPMailerAutoload.php';
    $mail = new PHPMailer;

    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host       = 'smtp.gmail.com'; // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true; // Enable SMTP authentication
    $mail->Username   = 'username@gmail.com'; // SMTP username
    $mail->Password   = 'password'; // SMTP password
    $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587; // TCP port to connect to
    $mail->setFrom('username@gmail.com', 'Name');
    $mail->addAddress($email, $username);
    $mail->isHTML(true);
    $mail->Subject = 'Subject';
    $mail->Body    = 'Message';
    !$mail->send();
 }
?>

Rest all database and other code is working correctly. When I only try to send sms using below code it sends only single message

<?php
    require_once 'smsapi/way2sms-api.php';
            sendWay2SMS('username', 'password', $uname,' Message');
?>

Or When I only try to send email using below code it sends only single email

<?php
       require_once 'mailapi/PHPMailerAutoload.php';
        $mail = new PHPMailer;

        $mail->isSMTP(); // Set mailer to use SMTP
        $mail->Host       = 'smtp.gmail.com'; // Specify main and backup SMTP servers
        $mail->SMTPAuth   = true; // Enable SMTP authentication
        $mail->Username   = 'username@gmail.com'; // SMTP username
        $mail->Password   = 'password'; // SMTP password
        $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
        $mail->Port       = 587; // TCP port to connect to
        $mail->setFrom('username@gmail.com', 'Name');
        $mail->addAddress($email, $username);
        $mail->isHTML(true);
        $mail->Subject = 'Subject';
        $mail->Body    = 'Message';
        !$mail->send();
    ?>

Android code

public void sendMessage(View view){
        StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL1,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        try {
                            Toast.makeText(EventDetailActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
                        }catch (Exception e){

                        }
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                String Status = ebtn.getText().toString().trim().toLowerCase();
                SharedPreferences sharedPreferences = getSharedPreferences(SharedPrefConfig.SHARED_PREF_NAME,Context.MODE_PRIVATE);
                String uname = sharedPreferences.getString(SharedPrefConfig.USERNAME_SHARED_PREF, null);
                Map<String,String> params = new Hashtable<String, String>();
                params.put("status", Status);
                params.put("uname", uname);
                params.put("dept", dept);
                params.put("ename", name);
                return params;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

Solution

  • Volley uses a RetryPolicy for processing requests which by defaults sends the request up to 3 times with an exponential backoff algorithm. So this is the actual problem.

    So I added a single line in my volley class

    stringRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    

    So, This resolved the problem.