Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading email with PHP 1

Status
Not open for further replies.

IB2

IS-IT--Management
Jun 7, 2003
3
US
I know you can use PHP to send emails but can you use PHP to read incoming emails? Is it a question of manipulating the directory where the emails are being received on the server?
 
i'd use the built in pop3 or imap functions to retrieve the mails rather than addressing the server's file system directly.
 
You want to use the IMAP() functions. They are fairly easy to use once you get the hang of them.

Here's a small program I just wrote to read a POP mailbox and insert records into a MySQL table.

Code:
$user = 'xxxx'; // pop3 user name
$pwd = 'xxxx';  // pop3 password
$hst = 'example.com'; // host address
$mbox = imap_open ('{'.$hst.':110/pop3}INBOX', $user, $pwd);
$headers = imap_headers($mbox);
if (imap_num_msg($mbox) == 0) exit();
// Include MySQL database information
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Unable to connect!");
$db = mysql_select_db($dbname);

for ($i = 1; $i <= imap_num_msg($mbox); $i++)
{
	$qtmp = array();
   $header = imap_headerinfo($mbox, $i, 80, 80);
	$msg_date = $header->Date;
	$from = $header->from;
	foreach ($from as $id => $object) {
	   $fromname = $object->personal;
	   $fromaddress = $object->mailbox . "@" . $object->host;
	}
//	$qtmp[] =  ($qtmp will be used to create the query 
   $messageBody = imap_body($mbox, $i);
	$tmp = explode(',',$messageBody);
	$q = 'insert into mysql_table set ';
	$q .= implode(',',$qtmp);
	echo $q."\n";
	mysql_query($q) or die(mysql_error());
	imap_delete($mbox,$i);
  }
imap_expunge($mbox);
imap_close($mbox);
?>

Some of the above code comes directly from php.net

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top