在PHP

时间:2017-08-01 13:28:59

标签: php loops debugging for-loop

我的代码中的'for'循环重复执行第一次遍历代码,然后再转到下一遍。循环应重复3次,但脚本在第一次重复时执行多次。当我在错误日志中看到以下内容时,我意识到了这一点:

  

[24-Jul-2017 15:03:42 America / New_York]发送至:*
[2017年7月24日   15:03:42 America / New_York] 1
[24-Jul-2017 15:03:48   America / New_York]发送至:*
[24-Jul-2017 15:03:49   America / New_York] 1周[24-Jul-2017 15:03:55 America / New_York]已发送   to:* [24-Jul-2017 15:03:55 America / New_York] 1
[2017年7月24日   15:04:01 America / New_York]发送至:*
[24-Jul-2017 15:04:01   America / New_York] 1周[24-Jul-2017 15:04:07 America / New_York]已发送   to:* [24-Jul-2017 15:04:08 America / New_York] 1
[2017年7月24日   15:04:14 America / New_York]发送至:*
[24-Jul-2017 15:04:14   America / New_York] 1周[24-Jul-2017 15:04:20 America / New_York]已发送   to:* [24-Jul-2017 15:04:20 America / New_York] 1
[2017年7月24日   15:04:26 America / New_York]发送至:*
[24-Jul-2017 15:04:26   America / New_York] 1
[24-Jul-2017 15:04:32 America / New_York]没有   收到的消息![24-Jul-2017 15:04:32 America / New_York] 2
  [24-Jul-2017 15:04:38 America / New_York]没有收到消息!
  [24-Jul-2017 15:04:38 America / New_York] 3

我让它在每个循环结束时将循环计数写入日志,你可以看到第一个重复多次。这是与我的循环相关的代码:

for ($count = 0; $count < 4; $count++) {

// Do STUFF HERE
// ERROR_LOG when a successful message has been sent

error_log($count);
sleep(5);
}

我不知道为什么会这样,因为代码对我来说没问题。这可能是服务器问题吗?或者其余代码中的某些东西会影响循环?如何让它循环正确的次数?

如果它可以使用,这是我正在使用的完整代码。它基本上只是将电子邮件附件转发到SQL数据库中的一组电子邮件地址。

// INCLUDE FUNCTIONS FOR OPENING EMAIL ATTACHMENT

include('attach.php');

// OPEN EMAIL AND CHECK FOR ATTACHMENT

for ($count = 0; $count < 4; $count++) {


/* connect to gmail */
$hostname = '***';
$username = '***';
$password = '***';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

$max_emails = 16;


/* if any emails found, iterate through each email */
if($emails) {

    $count = 1;

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) 
    {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);

        /* grab sender's phone number */
        foreach ($overview as $msgparts){
        $fromaddress = $msgparts->from;
        $fromnumber = explode("@", $fromaddress);
        $sendernumber = substr($fromnumber[0], -10);    
        }

    /* check if sender's number is in the database */   
$servername = "localhost";
$username = "***";
$password = "***";
$dbname = "***";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
   error_log("Connection failed: " . $conn->connect_error);
    goto end;
} 

$sql = "SELECT * FROM Members WHERE Phone_Number LIKE '$sendernumber'";
$result = $conn->query($sql); 

if (mysqli_num_rows($result) == 0) {    
$conn->close();
   error_log("Member number (" . $sendernumber . ") not recognized!");
    goto end;
}  else {
      $conn->close();
} 
} 

        $message = imap_fetchbody($inbox,$email_number,2);

        /* get mail structure */
        $structure = imap_fetchstructure($inbox, $email_number);

        $attachments = array();

        /* if any attachments found... */
        if(isset($structure->parts) && count($structure->parts)) 
        {
            for($i = 0; $i < count($structure->parts); $i++) 
            {
                $attachments[$i] = array(
                    'is_attachment' => false,
                    'filename' => '',
                    'name' => '',
                    'attachment' => ''
                );

                if($structure->parts[$i]->ifdparameters) 
                {
                    foreach($structure->parts[$i]->dparameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'filename') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['filename'] = $object->value;
                        }
                    }
                }

                if($structure->parts[$i]->ifparameters) 
                {
                    foreach($structure->parts[$i]->parameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'name') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['name'] = $object->value;
                        }
                    }
                }

                if($attachments[$i]['is_attachment']) 
                {
                    $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);

                    /* 3 = BASE64 encoding */
                    if($structure->parts[$i]->encoding == 3) 
                    { 
                        $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                    }
                    /* 4 = QUOTED-PRINTABLE encoding */
                    elseif($structure->parts[$i]->encoding == 4) 
                    { 
                        $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                    }
                }
            }
        }

        /* iterate through each attachment and save it */
        foreach($attachments as $attachment)
        {
            if($attachment['is_attachment'] == 1)
            {
                //error_log($attachment['name']);
                //error_log($attachment['attachment']);
                $filetype = substr($attachment['name'], -3);
                //$filename = $attachment['name'];
                if ( ($filetype == "txt") || ($attachment['name'] == "")) {
                $filename = "message.txt";
                if(empty($filename)) $filename = $attachment['filename'];

                if(empty($filename)) $filename = time() . ".dat";

                /* prefix the email number to the filename in case two emails
                 * have the attachment with the same file name.
                 */
                //$fp = fopen("./" . $email_number . "-" . $filename, "w+");
                $fp = fopen("./message.txt", "w+");
                fwrite($fp, $attachment['attachment']);
                fclose($fp);
                    }
                    else { 
                    //not a text message
                    }
           }
    }
}


// EXTRACT EMAIL ATTACHMENT TO TEXT FILE

If (file_exists("message.txt")) {

$emailmessage = fopen("message.txt", "r") or die("Unable to open file!");
$relaymessage = fread($emailmessage,filesize("message.txt"));
fclose($emailmessage);
unlink ("message.txt");
} else {
    error_log("No message received!");
    goto end;
}

// FORWARD ATTACHMENT TO ALL USERS IN DATABASE

If ($relaymessage != "") {

$servername = "localhost";
$username = "***";
$password = "***";
$dbname = "***";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    //die("Connection failed: " . $conn->connect_error);
   error_log("Connection failed: " . $conn->connect_error);
    goto end;
} 

$sql = "SELECT Address FROM Members";
$result = $conn->query($sql); 

if ($result->num_rows > 0) {    
    while($row = $result->fetch_assoc())  {
        $sent=normal($row["Address"], $relaymessage, "", 1);
        error_log("Sent to: " . $row["Address"]);
}
}  else {
      error_log("Error: " . $row["Address"]);
}

$conn->close();

} else {
    error_log("Message is blank");
    goto end;
}

end:
imap_delete($inbox,$email_number);
imap_expunge($inbox);
imap_close($inbox), CL_EXPUNGE);
error_log($count);
sleep(5);
}


function normal($to,$message,$oper,$num)
{
$adhead="";
for($i=1;$i<=$num;$i++)
{
$sent=mail($to, "", $message, $adhead);
}
if($sent)
{
echo "<h1>Mail sent successfully to $to</h1>";
}
else
{
echo '<h1>Mail not sent!</h1>';
}
}

1 个答案:

答案 0 :(得分:1)

你有问题:

if($emails) {    $count = 1;//<======

$count用于for循环,但已在内部人员if中进行了编辑!

所以你必须使用另一个变量名。