too long

I have a web app which has to send multiple emails. Right now I have the email addresses and passwords stored in a MYSQL database. I have displayed them on a html page and when I click on the link, I am redirecting to a php file that sends the email. I created a BULK-MAIL option where I have to click just one button and there will be multiple redirects from a page called bulk.php to send_mail.php and progressively sends emails for all the items in the database.

After sending like 15 mails or so, I get this error "localhost redirected you too many times."

I have to send 30 emails +/- 1 or 2.

How can I get rid of it ?

Thanks!

bulk.php file:

require('connections/conexiune.php');

$sql = "SELECT id, nume, email, parola, temp_trimis FROM firme ORDER BY nume DESC";
$res = $conn->query($sql);

$i=0;

if ($res->num_rows > 0) {
    // output data of each row
    while($firma = $res->fetch_assoc()){
        if($firma["temp_trimis"]==0){
            header("Location: send_email.php?nume=".$firma['nume']."&id=".$firma['id']."&email=".$firma['email']."&parola=".$firma['parola']."&blk=1");
        }else{
            $i++;
        }
    }
    $conn->close();
}
if($i==30) header("Location: index.php");

send_email.php file:

$luni = array('Ianuarie', 'Februarie', 'Martie', 'Aprilie', 'Mai', 'Iunie', 'Iulie', 'August', 'Septembrie', 'Octombrie', 'Noiembrie', 'Decembrie');
$luna = date('m');
$luna = strtoupper($luni[(int)$luna-2]) . " " . date('Y');

require('connections/conexiune.php');
require("libs/phpmailer/PHPMailerAutoload.php");

//preluarea de date
if(isset($_GET['nume'],$_GET['email'],$_GET['id'],$_GET['parola'])){
    $fields=[
        'nume_firma' => $_GET['nume'],
        'email' => $_GET['email'],
        'id' => $_GET['id'],
        'parola' => $_GET['parola'],
    ];
    if(isset($_GET['blk'])){
        $blk=$_GET['blk'];
    }else{
        $blk=0;
    }
}


//Atasament
foreach (glob("export/*") as $filename) {
    $k = strpos($filename, $fields['id']);
    if($k == TRUE){
        $att=$filename;
    }
}

//determinarea hostului(gmail sau yahoo) in functie de email    
$detHost=strpos($fields['email'],'gmail');

if($detHost == TRUE){
    $host="smtp.gmail.com";
}else{
    $host="smtp.mail.yahoo.com";
}

class Mailer extends PHPMailer {
    public function copyToFolder() {
        $message = $this->MIMEHeader . $this->MIMEBody;
        $imapStream = imap_open("{imap.mail.yahoo.com:993/imap/ssl}Sent", $this->Username , $this->Password);
        imap_append($imapStream, "{imap.mail.yahoo.com:993/imap/ssl}Sent", $message, "\\Seen");
        imap_close($imapStream);
    }
}


$m = new Mailer();


        $m -> isSMTP();
        $m -> SMTPAuth = true;

        $m -> Host = ''.$host.'';
        $m -> From = ''.$fields['email'].'';
        $m -> FromName = ''.$fields['nume_firma'].'';
        $m -> Username = ''.$fields['email'].'';
        $m -> Password = ''.$fields['parola'].'';
        $m -> SMTPSecure = 'ssl';
        $m -> Port = 465;
        $m->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $m ->isHTML();
        $m -> Subject = 'Raportare sumal luna '.$luna.' pentru '.$fields['nume_firma'].'';
        $m -> Body = 'Buna ziua!<p>
                        Va trimit in atasament raportarea sumal pentru luna '.$luna.' - '.$fields['nume_firma'];
        $m -> FromName = 'Aplicatie Raportari Sumal';
        $m->addAddress('raportariemapp@gmail.com');
        //$m->addAddress('sumal.mures@gmail.com');
        $m->addAttachment(''.$att.'');
        //$m->SMTPDebug = 4;

if(!$m->Send())
{

   echo "Mesajul nu a fost trimis ! <p>";
   echo "Mailer Error: " . $m->ErrorInfo;

   exit;
}else{
    if($blk==0){
        header("Location: index.php");
    }else{
        header("Location: bulk.php");
    }

    $sql = "UPDATE firme SET temp_trimis = 1 WHERE id = ".$fields['id']."";
    $res = $conn->query($sql);

    if($detHost==FALSE)
        $m->copyToFolder();
}

And the button is on the index file.

<button type="button" class="btn" onclick="location.href='bulk.php'">BULK-MAIL</button>