naeemqaswar
12/14/2016 - 9:52 AM

IMAP - Server Mails Reader

IMAP - Server Mails Reader

Other References:

- Imap PHP class
https://github.com/geerlingguy/Imap/blob/1.x/JJG/Imap.php

- Get the actual email message that the person just wrote, excluding any quoted text
http://stackoverflow.com/questions/7978987/get-the-actual-email-message-that-the-person-just-wrote-excluding-any-quoted-te/12611562#12611562?newreg=27f945d5d9494d6a88fd699cb1c799f4
<?php
error_reporting(1);

class Email_reader
{
    // imap server connection
    public $conn;

    // inbox storage and inbox message count
    private $inbox;
    private $msg_cnt;

    // email login credentials
    private $server = 'SERVER_NAME';
    private $user = 'USER_NAME';
    private $pass = 'PASSWORD';

    private $port = 110; // adjust according to server settings

    // connect to the server and get the inbox emails
    function __construct()
    {
        $this->connect();
//        $this->inbox();
    }

    // close the server connection
    function close()
    {
        $this->inbox = array();
        $this->msg_cnt = 0;

        imap_close($this->conn);
    }

    // open the server connection
    // the imap_open function parameters will need to be changed for the particular server
    // these are laid out to connect to a Dreamhost IMAP server
    function connect()
    {
        $this->conn = imap_open('{' . $this->server . '/notls}', $this->user, $this->pass);
    }

    // move the message to a new folder
    function move($msg_index, $folder = 'INBOX.Processed')
    {
        // move on server
        imap_mail_move($this->conn, $msg_index, $folder);
        imap_expunge($this->conn);

        // re-read the inbox
        $this->inbox();
    }

    // get a specific message (1 = first email, 2 = second email, etc.)
    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];
    }

    // read the inbox
    function inbox()
    {
        $this->msg_cnt = imap_num_msg($this->conn);

        $in = array();

//        for ($i = $this->msg_cnt; $i > 0; $i--) {         // Get messages in reverse order
        for ($i = 0; $i <= $this->msg_cnt; $i++) {
            $in[] = array(
                'index' => $i,
                'header' => imap_headerinfo($this->conn, $i),
                'body' => imap_fetchbody($this->conn, $i,"1"),
                'structure' => imap_fetchstructure($this->conn, $i)
            );

            /*
             * Testing Purpose Only: Below code will limit the mails to given counter
            */
//            if($i == $this->msg_cnt){
//                break;
//            }

        }

        $this->inbox = $in;

        return $in;
    }

    function extract_msg_without_quoted_reply($mail_content){
        // Stripping Out Quoted Text
        $mail_content = preg_replace('/.*((^>+\s{1}.*$)+\n?)+/mi', '', $mail_content);

        // Stripping Out Unwanted elements
        $mail_content = str_replace('>:','',$mail_content);
        $mail_content = str_replace('>','',$mail_content);
        $mail_content = str_replace('<','',$mail_content);
        $mail_content = str_replace('wrote:','',$mail_content);
        $mail_content = trim($mail_content);
        $mail_content = str_replace("'", "\\'", $mail_content);

        return $mail_content;
    }
}

$obj = new Email_reader;
echo "<pre>",print_r($obj->inbox()),"</pre>";die;