Search code examples
phpcodeigniteremailoutlookagenda

How to track email from to your codeigniter app


I'm writing an application with codeigniter, which is clearly done but I just want 1 more plugin in it. I would like to catch all the emails from my outlook account to my codeigniter app.

It would be awesome if I could send and receive messages on my codenigter app.

My second question is how can I api my agenda from my codeigniter app to the agenda from outlook?


Solution

  • Email works on some protocols for incoming (IMAP) and outgoing (SMTP) or similar like POP3 etc.

    So you must have configured those settings in outlook to read your mails and send mails. Similarly, You can read mails in PHP and Send mails using php.

    Sending Emails:

    You can use codeigniter core email library which works well for outgoing. sending emails codeigniter

    Reading Mails:

    This script can read your mail by providing configuration which you provided to outlook.

    <?php
    class Email_reader {
    
        // imap server connection
        public $conn;
        // inbox storage and inbox message count
        private $inbox;
        private $msg_cnt;
    
        // email login credentials
        private $server = 'YOUR_MAIL_SERVER';
        private $user   = 'email@mailprovider.com';
        private $pass   = 'yourpassword';
        private $port   = 143; // change 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 = 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;
        }
    }
    ?>
    

    Its a basic script to read mails, you can enhance it according to your requirement.