Chinmay Joshi
Chinmay Joshi

Reputation: 1

how do i solve the maximum execution time exceeded in php imap

I'm facing problem with imap. Actually i m trying to fetch some mails whose subject words matches with keywords in my database. I m using an email id on which it works fine, but while using different email id's with same mails in it, it is showing maximum execution time of 30 sec exceeded. I also checked the imap settings in gmail, they are alright..

And also getting this error.. "Undefined property: stdClass::$subject"

<?php
// Create connection
$con=mysqli_connect("localhost",'root',"","project 6 sem");

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//else
  //echo "Database connected..</br>";
?>
<?php

function Reply($prikey,$seckey,$submit3)
{
  $con=mysqli_connect("localhost",'root',"","project 6 sem");
  // Check connection
  if (mysqli_connect_errno())
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
    echo $prikey;
    echo $seckey;

   $result = mysqli_query($con,"SELECT * FROM data1 where pkey='$prikey' and skey='$seckey'");

  while($row = mysqli_fetch_array($result))
  {
       if($submit3 == "Positive")
       {
           $ans1 = $row['reply_yes'];

     //echo $ans1;
       }
       else if($submit3 == "Negative")
       {
           $ans1 =  $row['reply_no'];
     //echo $ans1;
       }
       echo "<br>";
       break;

    //header('location:'.$_SERVER['HTTP_REFERER']);
  }
   mysqli_close($con);
   return $ans1;
}
?>
<?php
$submit1 = NULL;
$submit2 = NULL;
if(isset($_POST['submit1']))
{
    $submit1 = ($_POST['submit1']);
}
//echo $submit1;
if(isset($_POST['submit2']))
{
    $submit2 = ($_POST['submit2']);
}
if(isset($_POST['select']))
{
    $select = ($_POST['select']);
}

$cbody=0;
$subject=0;
$hostname= '{imap.gmail.com:993/ssl}INBOX';
$username = 'xxxxx';
$password = 'XXXXX';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');

$count = imap_num_msg($inbox);
$temp=array();
$m=0;
$a=0;
$sk=array();
$pk=array();
$subcount=0 ;
$subarray=array() ;
$msgcount=0;
$msgarray=array() ;
$ans1 = "";
$b=0;
  if($inbox)
  {
       $num = imap_num_msg($inbox);
       rsort($emails);
       $pk = array();

       foreach ($emails as $email_number)
      {
      $b++;
      echo "$b</br>";
         // ini_set('max_execution_time',50);
          $headers = imap_headerinfo($inbox, $email_number);    
          $sub_arr=explode(" ",$headers->subject);
          $count_sub=count($sub_arr);
          $pkey=array("Permission","permission",
                                          "Application","application",
                      "Complaint","complaint",
                                          "Enquiry","enquiry",
                      );
           $skey=array("lab","Lab","auditorium","Auditorium","IT Parking","leave");                                                   
          for($j=0;$j<$count_sub;$j++)
          {
                if($a==4)
                    break;
            for($r=0;$r<count($pkey);$r++)
            {       
                if($a==4)
                    break;
                  if($sub_arr[$j]==$pkey[$r])// Find the Primary key i.e from Subject
                  {
              $a++;
                          $sub=$headers->subject;
              $subarray[$subcount]=$headers->subject;
              $subcount+=1;
                          //echo "<b>$headers->subject</b></br>";
                          $message=imap_fetchbody($inbox,$email_number,1);
                          $msgarray[$msgcount]=imap_fetchbody($inbox,$email_number,1);
                          $body=explode(" ",$msgarray[$msgcount]);
              $msgcount+=1;
                          $count=count($body);

Upvotes: 0

Views: 1242

Answers (2)

Sharikov Vladislav
Sharikov Vladislav

Reputation: 7269

ANSWER You can change max_execution_time parameter in php.ini file. Standart is 30. If you have access you can change it to needed value.

Source

BUT I don't think it is good idea (and also if you can't change your php.ini file).

Another way is to make php file, where you will get array of user's email. After that you will create ajax request, which will send each e-mail. So your script will work for only few seconds (or, possibly, ms), but many times. I think it is better (exactly if you can't edit php.ini, like in mine situation).

If you use this approach, this will allow you to get error's count, for example, and make ui more user friendly. You can track numbers of mails had already sent, for example.

I will add there simple example:

Script, which will get all e-mails and will be sending ajax requests:

foreach(email in emails) {
?>
    <script type="text/javascript">
        $.ajax({
            url: "send_rent_receipts.php",
            type: 'POST',
            data: 'mail='+email, // and other data
            success: function(data){
                count = count + 1;
                if(data == 'true')
                {   // add successfull sent
                    countSuccess = countSuccess + 1;
                }
                else
                {   // add error
                    arr.push(data); 
                    countErrors = countErrors + 1;
                }
                if(count == <?=$totalEntries;?>)
                {
                    $('#status').html('All mails had been sent!<br>Sent successfully: '+countSuccess+', Errors:  '+countErrors);
                    $("#status-image").attr( "src", "/images/thumb_up.png" );
                    for (key in arr) 
                    {
                        $('results').append(arr[key]+"<br>"); // handling errors
                    }
                }
                else
                {
                    $('#status').html('Process...<br><b>Total e-mail's: '+totalEntries+'</b><br>Sent successfully: '+countSuccess+', Errors: '+countErrors);
                }
            }
        });
    </script>
<?
}

Script which will send 1 e-mail:

// send 1 e-mail

Simple :)

But, you can just change max_execution_time :)

Upvotes: 1

DragonYen
DragonYen

Reputation: 968

If you can't change the INI, you can set the setting in your script. This might be safer for a single script instead of changing it for all scripts on the server (as an INI change would).

<?php
    set_time_limit ( 0 ); //0 = unlimited

For more information : http://www.php.net/manual/en/function.set-time-limit.php

Upvotes: 1

Related Questions