reading emails with php code example
Example: read an email with php
<?php
class Email_reader {
public $conn;
private $inbox;
private $msg_cnt;
private $server = 'yourserver.com';
private $user = '[email protected]';
private $pass = 'yourpassword';
private $port = 143;
function __construct() {
$this->connect();
$this->inbox();
}
function close() {
$this->inbox = array();
$this->msg_cnt = 0;
imap_close($this->conn);
}
function connect() {
$this->conn = imap_open('{'.$this->server.'/notls}', $this->user, $this->pass);
}
function move($msg_index, $folder='INBOX.Processed') {
imap_mail_move($this->conn, $msg_index, $folder);
imap_expunge($this->conn);
$this->inbox();
}
function get($msg_index=NULL) {
if (count($this->inbox) <= 0) {
return array();
}
elseif ( ! is_null($msg_index) && isset($this->inbox[$msg_index])) {
return $this->inbox[$msg_index];
}
return $this->inbox[0];
}
function inbox() {
$this->msg_cnt = imap_num_msg($this->conn);
$in = array();
for($i = 1; $i <= $this->msg_cnt; $i++) {
$in[] = array(
'index' => $i,
'header' => imap_headerinfo($this->conn, $i),
'body' => imap_body($this->conn, $i),
'structure' => imap_fetchstructure($this->conn, $i)
);
}
$this->inbox = $in;
}
}
?>
A fair amount of this is