PDA

View Full Version : reading inbox



xtendedf
11-12-2007, 10:39 PM
trying to script my inbox with not much success.

#!/usr/bin/php -q
<?php
$MAILSERVER="{localhost}";
$link=imap_open($MAILSERVER,"user","password");
$header=imap_header($link,$num);
if (!$header) echo("could not retrieve headers");
echo "From: $header[fromaddress]<br>";
echo "To: $header[toaddress]<br>";
echo "Date: $header[Date]<br>";
echo "Subject: $header[Subject]<br><br>";
echo imap_body($link,$num);
?>

tried {mail.mydomain.com} no go.
tried @imap_open - nope
.....

any suggestions? alex

xtendedf
11-15-2007, 12:05 PM
script that reads an email survey result:

suppose you posted a notice somewhere:

questions:

1. your favorite color?
2. your age?
3. your city?
4. your name?
5. your iq?

respond(email) with with the following:

<start>
color,age,city,name,iq
<end>


this script will read the email and respond back.
you could do other things like add to a database etc.
put the script in the crontab and the possibilities are
endless. careful though, the email responses are deleted.

#!/usr/bin/php -q
<?php
$link=imap_open("{localhost/imap}","user","password");
$count=imap_num_msg($link);
for($x=1;$x<=$count;$x++) {
$header = imap_header($link,$x);
$from=$header->fromaddress;
$chunk = explode("<start>",imap_body($link, $x));
if ($chunk[1]) {
$data = explode("<end>",$chunk[1]);
if ($data[0]) {
$s = "Your survey says:\n";
$tokens = explode(",",$data[0]);
for ($i=0;$i<count($tokens);$i++) {
$s = $s . "token$i=$tokens[$i]\n";
}
if (mail($from,"survey result", $s, "From:surveyor@here.com")) {
echo("Message successfully sent! to " . $from . "\n");
} else {
echo("Message delivery failed! to " . $from . "\n");
}
imap_delete($link,$x);
}
}
}
imap_close($link,CL_EXPUNGE);


useful?

alex

gynn
11-15-2007, 01:12 PM
In resolving issues like this, logfile info is usually more helpful than source code. Could be a permissions problem. It's also not clear if/how the $num variable gets a value.

Need more info to be helpful I think..

xtendedf
11-15-2007, 01:17 PM
My last post was supposed to be the
solution, in case somebody was interested.........

sjlplat
11-16-2007, 08:11 AM
Nice code! I've considered writing an IMAP autoresponder, but I never got passed the brainstorming phase. I'll have to play with this. Thanks for posting it! :cool: