-1

I have a table as

<tr>
                                <td><?php echo ++$count ?> </td>
                                <td style="text-align:center;"><input type="checkbox" name="id[]" id="id[]" class="checkboxes" value="<?php echo $row['id']; ?>" ></td>
                                <td><?php echo $row['fname'] ?></td>
                                <td><?php echo $row['mobile'] ?> </td>
                                <td><?php echo $row['email'] ?> </td>
                                <td><?php echo $row['regdate'] ?> </td>
                            <td><button value="<?php echo $row['id'] ?>" id="userdelete" class='userdelete btn btn-danger'>Delete</button></td>
                            </tr>

where i get userid, fullname, mobile number, email from database.

i want to select multiple users using checkbox and send message using phpmailer. iam using Ajax for this.

$(document).on('click', '#sendemail', function(ev){
                ev.preventDefault();
            var SlectedList = new Array();
            $("input[name='id[]']:checked").each(function() {
                SlectedList.push($(this).val());
            });

            $emailbody=$('#emailbody').val();
            $emailheader=$('#emailheader').val();           
                $.ajax({
                    type: "POST",
                    url: "subscribehere.php",
                    data: {
                        emailbody: $emailbody,
                        emailheader: $emailheader,
                        id: SlectedList,
                        sendemail: 1,
                    },
                    success: function(response){
                        $('.subscriber_msg').html(response);
                        $('#emailModal').modal('hide');
                    }
                });
        });

and my php code is

<?php
$errmsg = " ";
if (isset($_POST['sendemail'])) {
    $header=$_POST['emailheader'];
    $ebody=$_POST['emailbody'];

    if ( count($_POST['id']) > 0 ) {
        foreach($_POST['id'] as $val){

            $sql = "SELECT * FROM subscribers WHERE id=$val ";
            $query=mysqli_query($conn,$sql) or die(mysqli_error($conn));

            if ( $row1= mysqli_fetch_assoc($query) ) {
                $email=$row1['email'];
                $mobilenumber=$row1['mobile'];
                require 'class/class.phpmailer.php';
                $mail = new PHPMailer;
                $mail->IsSMTP();                                //Sets Mailer to send message using SMTP
                $mail->Host = 'smtpout.secureserver.net';       //Sets the SMTP hosts of your Email hosting, this for Godaddy
                $mail->Port = '80';                             //Sets the default SMTP server port
                $mail->SMTPAuth = true;                         //Sets SMTP authentication. Utilizes the Username and Password variables
                $mail->Username = '***********';                    //Sets SMTP username
                $mail->Password = 'vvnagar123';                 //Sets SMTP password
                $mail->SMTPSecure = '';                         //Sets connection prefix. Options are "", "ssl" or "tls"
                $mail->From = '********';                   //Sets the From email address for the message
                $mail->FromName = 'Radio SAREGAMA';             //Sets the From name of the message
                $mail->AddAddress($email, $mobilenumber);       //Adds a "To" address
                $mail->WordWrap = 50;                           //Sets word wrapping on the body of the message to a given number of characters
                $mail->IsHTML(true);                            //Sets message type to HTML             
                $mail->Subject = $header;               //Sets the Subject of the message
                $mail->Body = $ebody;
                                //An HTML or plain text message body
                $mail->Send();
                exit;
            } else {
                echo "Error while sending.". mysqli_error();    
            }
            $conn->close();
        }
        echo "totally ".count($_POST['id']). " Emails has been sent successfully";
    } else {
        echo "You need to select atleast one checkbox to delete!";
    } 
}
?>

when i select one user, email sent successfully but when select multiple users its only sending the email to first user and ignore the remaining. where am doing wrong step plz help...

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
G Karthik
  • 7
  • 2

1 Answers1

0

Um, this is pretty simple; you're calling exit after sending the first message, so your script will simply stop there! Delete that line and it will be able to send all your messages.

Also, this is very inefficient code - take a look at the mailing list example provided with PHPMailer for how to send to lists.

You're also running a very old version of PHPMailer, so get the latest version.

Synchro
  • 35,538
  • 15
  • 81
  • 104