I have figured most of this PHP/IMAP stuff, but am stuck.
Here is what I am doing.
I am modifying a script to connect to IMAP to open a POP email box. (Works Fine).
I then run it through some parsing and insert parts of the email into a MySQL database table.
This all works fine. The page connects to the POP box, parses the email and then inserts the parsed sections (Email From Address, Body and Subject) into the MySQL table.
The probem I have is when there are more than 2 emails waiting to be parsed. The 2nd email is skipped and email 3 is put in its place, into the database. I guess my code is not iterating properly.
Ex. 3 emails waiting to be parsed in the POP box, Email 1, Email 2, Email 3.
After my code parses the 3 emails, the following is inserted into the database ..
Email 1
Email 3
Email 3
For some reason, email 2 is skipped and Email 3 is put in its place.
My code is below, if someone with some PHP skills could please take a look and help me out, I will be grateful. I am into my second week without a solution.
Thanks.
======= My Code ============
<?
//=============================================
/ Open DB Connection To email table /
$DBhost3 = "localhost";
$DBuser3 = "dbusername";
$DBpass3 = "dbpassword";
$DBName3 = "dbname";
$table3 = "email_table";
mysql_connect($DBhost3,$DBuser3,$DBpass3) or die("Unable to connect to database");
mysql_select_db("$DBName3") or die("Unable to select database $DBName");
{
$sqlquery = "SELECT * FROM $table3 ORDER BY name";
$result = mysql_query($sqlquery);
$number = mysql_numrows($result); $i = 0;
/ If No tickets, print this /
if ($number < 1) {
print "<font face='verdana' size='1' color='red'><b>Error: There do not appear to be any POP3 accounts configured. Did you enter them in the Admin section?</b></font>";
}
/ If there are open tickets, list them /
else {
while ($number > $i) { $theid =
mysql_result($result,$i,"id"); $thename =
mysql_result($result,$i,"name"); $thepophost =
mysql_result($result,$i,"pophost"); $thepopuser =
mysql_result($result,$i,"popuser"); $thepoppass =
mysql_result($result,$i,"poppass");
$MAILHOST = "$thepophost"; // Part of the email address after the "@"
$USER_NAME = "$thepopuser"; // The part of the email address before the "@"
$USER_PASS = "$thepoppass"; // Email password
PRINT "<br>M. $MAILHOST <br>U. $USER_NAME <BR>P. $USER_PASS<br>";
$i++;
}
}
}
/ Close the database connection /
MYSQL_CLOSE();
// =========================================
// THE CODE ABOVE WORKS WITHOUT A PROBLEM
// THE PROBLEM SEEMS TO BE BELOW AFTER SECTION 1A
// ========================================
// INCLUDE SOME VARIABLES
include("inc.e.imap.php");
// Begin section 1A
// PROCESS ONLY IF TICKETS ARE WAITING
for($total = 1; $total > 0; $total--){ // Open B-1
$x=$total;
// OPEN THE MAILBOX
$inbox = imap_open ("{". $MAILHOST . "/pop3:110}", $USER_NAME, $USER_PASS) or header("Location: imaperror.php");
// RETRIEVE NUMBER OF MESSAGES
$total = imap_num_msg($inbox);
// HEADER AND STRUCTURE
$headers = imap_header($inbox, $x);
$structure = imap_fetchstructure($inbox, $x);
// ========================
// FIND OUT IF EXISTING TICKET
// ========================
$TruncID = substr($TruncV, 2, 5); // Should Show [#
// The following is for TESTING
// ============================================
// IF TICKET IS EXISTING, PROCESS THE FOLLOWING
// ============================================
if($Trunc == "[#") { // B-2
$from=$headers->from;
$fromaddr=sprintf("%s@%s",$from[0]->mailbox,$from[0]->host);
$SUBJECT = $headers->Subject;
$FROM = $fromaddr;
$DATE = @substr($headers->Date, 0, 22);
$TECH = @ceil(($structure->bytes/1024));
$BODY = @htmlspecialchars(stripslashes(trim(imap_body($inbox, $x))));
include("inc.e.db.checklogin.php"); // CHECKS LOGIN FOR EXISTING USER
include("inc.e.db.tickets-update.php"); // IF EXISTING USER, UPDATES TICKET
// The following will delete the email after it is parsed
$check = @imap_mailboxmsginfo ($inbox);
imap_delete ($inbox, 1);
imap_expunge ($inbox);
imap_close ($inbox);
} // Close B-2
else{ // B-3
// =======================================
// IF TICKET IS NEW, PROCESS THE FOLLOWING
// =======================================
$from=$headers->from;
$fromaddr=sprintf("%s@%s",$from[0]->mailbox,$from[0]->host);
// print "<br>$fromaddr<br>"; // testing
$SUBJECT = $headers->Subject;
$FROM = $fromaddr;
$DATE = @substr($headers->Date, 0, 22);
$TECH = @ceil(($structure->bytes/1024));
$BODY = @htmlspecialchars(stripslashes(trim(imap_body($inbox, $x))));
// THE FOLLOWING CODE WILL PREVENT A NEW TICKET FROM BEING CREATED
// IF THE TICKET IS A RESULT OF AN AUTORESPONSE FROM DAPPER DESK
if($SUBJECT != "" AND $SUBJECT != "*** New Support Ticket ***" AND $SUBJECT != "*** New Support Account ***"){ //B-4
include("inc.e.db.checklogin.php"); // Checks for existing user
include("inc.e.db.insert.client.php"); // If no existing user, inserts user info in db
include("inc.e.db.tickets-insert.php"); // Inserts parsed sections of the email (From, Body, Subject)
} // Close B-4
// THE FOLLOWING CODE WILL DELETE THE EMAIL AFTER PARSING
$check = @imap_mailboxmsginfo ($inbox);
imap_delete ($inbox, 1);
imap_expunge ($inbox);
imap_close ($inbox);
} // Close B-3
} // Close B-1
?>