1

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);
    }
  • 2
    That's weird, but I'd bet your script is being run three times, probably by browser plugins. You can check - append a random number to the end of your message - if they are different in each message, you know your script is being run three separate times. If they are the same, you'll know your single message is being sent three times. – Synchro Apr 10 '17 at 17:07
  • Comment out the send sms part, and enter your email as receiver check if you receive 3 emails. If you do well then your code is running it 3 times. Then it's most probably a loop somewere repeating the task multiple times. Go step by step backwards and see where it happens. – Andrew Larsen Apr 10 '17 at 17:16
  • yes its looping 3 times. when i comment sendsms code then also i receive 3 email with same message. I'm using android at front end and sending request using volley library to server (Hope this may useful). – Tejaram Sutar Apr 10 '17 at 17:39
  • Right, so you know that it's not your PHP code that's the problem, it's the way it's being called, so check that end of the operation. Don't try to test your whole stack at once - test each part separately first, check your web server logs to see the multiple requests. – Synchro Apr 10 '17 at 17:57
  • Volley uses a **RetryPolicy** for processing requests which by defaults sends the request up to _3 times_ with an exponential backoff algorithm. I think this is the actual problem. – Tejaram Sutar Apr 10 '17 at 18:27

1 Answers1

0

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.