0

i have problem with loop.

i have code like this,

foreach ($j as $empas) {


    date_default_timezone_set('Asia/Jakarta');
    $nomor = Nomor($i, $total);
    $proses = proses($total, $i) . "%";
    $timeServer = date("G:i:s");
    $toDay = date("d-M-Y");
    for($p=0;$p<$total;$p++){
        $user = $userlist[$p%count($userlist)] ;


        echo "\e[37m [" . $timeServer . "] - {$nomor}/{$total} - [{$proses}] {$user} > {$empas}  \e[0m ";;
        if (isEmail($empas)) {
            echo imakochan($empas, $letter, $host, $user, $pw, $port, $sendermail, $sendername, $subjek). "\n";
            } else {
        echo " This is not valid email ";
    }

    if ($i == $total) {
        echo "\n";
        echo " [0;34m ================================================================================== \n";
        echo " [0;34m [+]            Sending Proccess Completed on " . $timeServer . "            [+]\n";
    }
     $i++;
 } 
}

so , my problem is i can't rotate smtp when the php mailer send email.

i want smtp rotate by email ,it should be rotate by email

like this

smtpuser1 > email1 ->sent!

smtpuser2 > email2 ->sent!

smtpuser1 > email3 ->sent!

smtpuser2 > email4 ->sent!

when i do like this, i've got problem :

[17:01:42] - 1/2 - [50%] smtpuser1 > [email protected]   -> Sent!

[17:01:42] - 1/2 - [50%] smtpuser2 > [email protected]   -> Sent!

  ==================================================================================
  [+]            Sending Proccess Completed on 17:01:42            [+]
 [17:01:50] - 3/2 - [150%] smtpuser1 > [email protected]   -> Sent!
 [17:01:50] - 3/2 - [150%] smtpuser2 > [email protected]   -> Sent!

1 Answer 1

0

Don't do a double loop for this. Think, when you use a double loop (loop inside a loop), the loop inside will run completely on each run of the external loop.

Example:

loop1 : run 5 times
  loop2: run 2 times

Result:

loop1 run1
  loop2 run1
  loop2 run2
loop1 run2
  loop2 run1
  loop2 run2
loop1 run3
  loop2 run1
  loop2 run2
loop1 run4
  loop2 run1
  loop2 run2
loop1 run5
  loop2 run1
  loop2 run2

To accomplish what are you trying, use a more simple approach. For example:

date_default_timezone_set('Asia/Jakarta');
$smtpArray = array(0 => 'smtp1.example.com', 1 => 'smtp2.example.com', 2 => 'smtp3.example.com');
$smtpCount = count($smtpArray);
$i = 0;
foreach ($j as $empas) {
    $nomor = Nomor($i, $total);
    $proses = proses($total, $i) . "%";
    $timeServer = date("G:i:s");
    $toDay = date("d-M-Y");
    $iteration = $i % $smtpCount; // Create a number of SMTP server to iterate it.
    $smtp = $smtpArray[$iteration]; // Use the SMTP server selected before.

    echo "\e[37m [" . $timeServer . "] - {$nomor}/{$total} - [{$proses}] {$smtp} > {$empas}  \e[0m ";
    if (isEmail($empas)) {
        echo imakochan($empas, $letter, $host, $smtp, $pw, $port, $sendermail, $sendername, $subjek). "\n";
    } else {
        echo " This is not valid email ";
    }

    if ($i == $total) {
        echo "\n";
        echo " [0;34m ================================================================================== \n";
        echo " [0;34m [+]            Sending Proccess Completed on " . $timeServer . "            [+]\n";
    }
    $i++; // Increase $i to use next SMTP server on next iteration.
}

I've get out the timezone set because you didn't need to set every run of the loop.

Sign up to request clarification or add additional context in comments.

7 Comments

what function for $user ?
I've forgot to delete $user variable. I've replace it with $smtp variable, more descriptive for their use.
in my code, i use $i = 1;, but u use it for 0. how i change it ? because i use it for print complete banner.
It's OK if you start $i on 1, because, on my code I'm using $i % $arrayCount, who returns the rest of the division.
so i make int $p = 0; but the result is PHP Parse error: syntax error, unexpected '$p' (T_VARIABLE)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.