Jump to content

Recommended Posts

Hey guys, somewhat new to PHP here, trying to get this project rolling. I have this script that checks an email address and returns the newest email in a table, works great. The problem is when I want to check multiple emails. I tried to put the script in a for loop and all it does it check the first email. It throws no errors, just doesn't work. Think anyone could help me out? Also, take into account I'm brand new, so my code is very messy. Thanks!

 

<?php 
include ('ID.php');
include ('secVar.php');

for ($i=0; $i <= 1; $i++)
{
$inbox = imap_open($hn[$i],$un[$i],$pw[$i]) or die('Cannot connect to database: ' . imap_last_error());

$emails = imap_search($inbox,'ALL');

if($emails) {

rsort($emails);

foreach($emails as $email_number) {

	$overview = imap_fetch_overview($inbox,$email_number,0);
	$message = imap_fetchbody($inbox,$email_number,2);
	$body[] = imap_fetchbody($inbox,$email_number,2);

	$date[] = ($overview[0]->date);
	$from[] = ($overview[0]->from);
	$subject[] = ($overview[0]->subject);
	$body[0];

}

$ID=assignID($from[0], $ID);

$con = mysql_connect("localhost","table","pw");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("db", $con);

$sql="INSERT INTO deals (ID, dat, `from`, subject, body)
VALUES
('$ID', '$date[0]','$from[0]','$subject[0]','$body[0]') ON DUPLICATE KEY UPDATE subject = '$subject[0]'";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

}

}

 

This is a fictional project that may evolve into a real thing for work. Basically what we want is to give each customer of ours an email to send items to, and have the last email they sent always sitting in our database. Sounds weird, but our service relies solely on what their last email says. So the idea is to have the PHP check about 10 different emails, and pull each of the latest emails and throw them in the table. That's why on each loop I'm storing $from[0], $subject[0], etc. Since I sorted them by time I only care about the info in the 0 array slot.

 

The problem is it's only storing the data from the first loop. I have verified that it's actually looping through all of the mailboxes, because if I echo a query to check amount of emails in the current mailbox it will spit out a handful of different numbers, all correlating properly to the given mailbox for the loop it's on. The problem is when it gets to the SQL insert, it just isn't inserting past the first loop.

Link to comment
https://forums.phpfreaks.com/topic/264180-for-loop-to-check-email/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.