I have done it a few times, and is always kind of a pain. you should also log into the mailbox once in awhile to check on it. Also, remember you will have spam, bad formatted emails, reply all emails in there so account for that.
also fun decoding the email body as it comes in a million flavors, with embedded graphics as well.
This is what I used to monitor a mailbox that had text based alerts.
class someEmailClass
function inbox() {
if( DEBUG) $GLOBALS['log']->Info( __CLASS__ .'.'. __FUNCTION__ .'()' );
$this->msg_cnt = imap_num_msg($this->conn);
$in = array();
imap_headers( $this->conn); // to boost performance somehow
$emails_to_fetch = $this->batchSize;
if( $this->batchSize > $this->msg_cnt)
// if fewer inbox items than batch size
{
$emails_to_fetch = $this->msg_cnt;
}
for($i = 1; $i <= $emails_to_fetch; $i++) {
$email = array(
'index' => $i,
'header' => imap_headerinfo($this->conn, $i),
'structure' => imap_fetchstructure($this->conn, $i)
);
$body = imap_fetchbody( $this->conn, $i, "1.1");
if ($body == "") {
$body = imap_fetchbody( $this->conn, $i, "1");
}
$email['body'] = $body;
$email['body2'] = self::email_body_decode( $body, (int)$email['structure']->encoding );
$in[] = $email;
}
$this->inbox = $in;
$this->Errors = imap_errors();
}
public static function email_body_decode( $data, $code =0)
{
switch( $code){
// case 0:
// return imap_utf7_decode($data);
case 1:
return imap_utf8($data);
case 3:
return imap_base64($data);
case 4:
return imap_qprint($data);
default:
return $data;
}
}
// http://www.php.net/manual/en/function.imap-open.php
function connect_debug()
{
try {
$this->conn = (imap_open("{mail.domain.com:993/imap/notls/ssl/novalidate-cert}INBOX", <user>, <pass>));
if( $this->conn == false) {
$err = 'Could not connect to email server ['. imap_last_error() .']';
$this->Errors[] = $err;
throw new \Exception( $err);
}
}
catch( \Exception $ex){
if( DEBUG) $GLOBALS['log']->Error( $ex );
throw $ex;
}
}
}
it is not complete, but will let you play with the message body at least after you figure out how to connect to the mailbox on the email server