Use PHP to open a POP3 or IMAP mailbox?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Use PHP to open a POP3 or IMAP mailbox?

Post by Chris Corbyn »

Hi,

Just like it says in the subject line.... is it possible for PHP to open an email inbox?

I'm not aiming to write some huge email application here. I just wanted a script to check an email inbox for emails with a particular subject line and carry out and log the subject line in a database if it isn't already there.

Don't even need to open the email body really - just a case of a regexp match on the subject lines.

I was hoping there was a way to do it without adding any extra components/libraries to PHP. If not, what do I need?

Cheers all :-)
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

yes...there is an example here

http://www.weberdev.com/get_example-4015.html
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

thanks. i'll start scourin my way through the endless lines of code to find the bits i need :-D
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i suggest oyu lookinto imap functions in the manual, that script uses fsock and all that good stuff, there is a list of mail functions like imap_open()
and stuff like that
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

That sounds good. Didn't realise there were such functions. I'd prefer to use imap too. Like I say, I wasn't intending to get into a lot of laborious coding just for an authentication system.

If I could just open a mailbox simply and loop over the subjects that would be good enough. Something on the same complexity, or not greatly more complex than using mysql or some of the advanced file functions.

If I really do have to script heavy code I'll do it but it'd be better if there is a shortcut.

I'll let you know how it goes and send some code back :-D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Off to a brilliant start LOL :-D

Tried the basic function:

Code: Select all

<?php
$mbox = imap_open("{imap.my_mail_server.com}INBOX","my_username","my_password") or die ('You fool!');
?>
Nothing else in the file and when I run it I get
Fatal error: Call to undefined function: imap_open() in C:\Webs\dct4\imap.php on line 2
I'm using PHP version 4.3.6 and it says in the manual the function is available in versions 3 and 4.

Hmmmm....
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

in php.ini you have to enable that set of functions

Code: Select all

;extension=php_bz2.dll
;extension=php_cpdf.dll
;extension=php_crack.dll
;extension=php_curl.dll
;extension=php_db.dll
;extension=php_dba.dll
;extension=php_dbase.dll
;extension=php_dbx.dll
;extension=php_domxml.dll
;extension=php_exif.dll
;extension=php_fdf.dll
;extension=php_filepro.dll
extension=php_gd2.dll
;extension=php_gettext.dll
;extension=php_hyperwave.dll
;extension=php_iconv.dll
;extension=php_ifx.dll
;extension=php_iisfunc.dll
extension=php_imap.dll<<<this is the one that you have to remove the semi-colon before
;extension=php_interbase.dll
;extension=php_java.dll
;extension=php_ldap.dll
;extension=php_mbstring.dll
;extension=php_mcrypt.dll
;extension=php_mhash.dll
;extension=php_mime_magic.dll
;extension=php_ming.dll
;extension=php_mssql.dll
;extension=php_msql.dll
;extension=php_oci8.dll
extension=php_openssl.dll
;extension=php_oracle.dll
;extension=php_pdf.dll
;extension=php_pgsql.dll
;extension=php_printer.dll
;extension=php_shmop.dll
;extension=php_snmp.dll
;extension=php_sockets.dll
;extension=php_sybase_ct.dll
;extension=php_w32api.dll
;extension=php_xmlrpc.dll
;extension=php_xslt.dll
;extension=php_yaz.dll
;extension=php_zip.dll
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Nice one cheers. I was just thinking that ;-) Hope the server I'm gonna upload the script to has them enabled :?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

with any luck they will....
i was just thinking about that, im working on a imap mail viewing class and im hoping my server has it all enabled
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'm still just testing various outputs of the imap functions.

I want to be able to extract various parts of the header and run pattern matches on them. Uisng the imap_header function you get all the details.

I used print_r to pull some deatils out of the email and display them but I cannot work out how to pull out just the subject and the sender.

What is "stdClass object"... looks like an array?

Code: Select all

<?php
$mbox = imap_open("{imap.ntlworld.com:143}", "myname", "mypass");

echo "<b><u>Details of Message Number ".$_GET['msgnum']."</u></b>\n";
$headers = imap_header($mbox,$_GET['msgnum']);
echo '<b>'.$headers[subject].'</b><p>';
echo '<pre>';
print_r($headers);
echo '</pre>';

imap_close($mbox);
?>
Gives this output:
Details of Message Number 27

stdClass Object
(
[date] => Mon, 1 Nov 2004 09:08:19 -0800
[Date] => Mon, 1 Nov 2004 09:08:19 -0800
[subject] => Question about payment for item #5134383813 - Phillips (<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>) 48/16 DVD-ROM Drive (No Reserve)
[Subject] => Question about payment for item #5134383813 - Phillips (<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>) 48/16 DVD-ROM Drive (No Reserve)
[message_id] => <998070348.1099328899219.JavaMail.ebayapp@sj-v3conta08>
[toaddress] => c.a.corbyn@ntlworld.com
[to] => Array
(
[0] => stdClass Object
(
[mailbox] => c.a.corbyn
[host] => ntlworld.com
)

)

[fromaddress] => keithemberton@hotmail.com
[from] => Array
(
[0] => stdClass Object
(
[mailbox] => keithemberton
[host] => hotmail.com
)

)

[reply_toaddress] => keithemberton@hotmail.com
[reply_to] => Array
(
[0] => stdClass Object
(
[mailbox] => keithemberton
[host] => hotmail.com
)

)

[senderaddress] => keithemberton@hotmail.com
[sender] => Array
(
[0] => stdClass Object
(
[mailbox] => keithemberton
[host] => hotmail.com
)

)

[Recent] =>
[Unseen] =>
[Flagged] =>
[Answered] =>
[Deleted] =>
[Draft] =>
[Msgno] => 27
[MailDate] => 1-Nov-2004 17:08:15 +0000
[Size] => 14661
[udate] => 1099328895
)
I tried this

Code: Select all

echo $headers[subject];
But nothing is displayed.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Got it.

Code: Select all

$headers->subject
OK I'm on my way to writing this script now. Just thinking I don't wanna get too carried away with it cos I have an Open University deadline in 2 weeks and I haven't even started it when Ihave had 6 weeks so far :?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I said that I'd post the code I wrote in here so here it is. It opens my mailbox, checks for emails confirming paypal payments, adds the item number to a database if it doesn't exist and emails the user a signup URL.

I'll be making one more tweak to it which will be to check that the item number is for the correct product (a database query again).

Code: Select all

<?php

$mysql_conn = mysql_connect('localhost','root','');
mysql_select_db('paypal',$mysql_conn) or die('Database error');
$get_item_nums = "SELECT * FROM users";
$return_item_nums = mysql_query($get_item_nums);
$num_rows = mysql_num_rows($return_item_nums);

$existing_item = array();
for ($i=0; $i<$num_rows; $i++) {
	$existing_item[mysql_result($return_item_nums,$i,'item')] = true; //Get item nums already added into array
}

$mbox = imap_open("{imap.ntlworld.com:143}", "xxxx", "xxxx"); //Open mailbox
$summary = imap_headers($mbox); //All messages

for ($i=1; $i<=count($summary); $i++) { //Loop over all messages
	$header = imap_header($mbox,$i); //Full header
	if(preg_match('/^Item\s*\#(\d*)\s-\sNotification\sof\san\sInstant\sPurchase\sPayment\sReceived\s*from\s*(\w*)/',$header->subject, $patt_match)) { // Check Subject is right email
		$subject = $header->subject;
		$item_num = $patt_match[1];
		$buyer_email = $header->reply_to[0]->mailbox.'@'.$header->reply_to[0]->host; //Makes buyer email from mailbox and host in 'reply to'
		
		$add_trans = "INSERT INTO users (item,email) VALUES ('$item_num','$buyer_email')";
		if (!array_key_exists($item_num,$existing_item)) { //If item num not already added to DB
			mysql_query($add_trans) or die('Data entry error'); //Record item num
			
			$msg_body = //My meassge here
			
			mail("$buyer_email",'Your Nokia DCT4 Unlocking account setup (eBay Item #'.$item_num.')',"$msg_body","MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\nFrom: <NokUnlock New account> mailbox@ntlworld.com\r\n");
			echo $buyer_email;
		}
	}
}

imap_close($mbox);

mysql_close($mysql_conn);

?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

so the trick would be to send a fake paypal payment mails to your system? ;))
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You got me sussed dude. Think I'm gonna encrypt the item numbers so it's not so easy. In all honesty it's not gonna attract that much attention to worry about hackers. At present if you went on eBay and found the item numbers of the stuff I'm listing then sent an email to my account with the appropritate subject and that item number then you'd receive a reply and could signup for a service. That's true. I'll just do some (secret) mathematical calculation on the item number and it wont be so easy ;-)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Oh thinking about it. I need the pattern match to work so I can't play with that bit.

I'll fix it somehow anyway :?
Post Reply