PHP Library to read email
Asked Answered
L

2

17

I currently use the SwiftMailer library to send email, but unfortunately it's only for sending, not receiving. I'm wondering... is there a similar library to connect via IMAP to an email account and read the email (IE give me the ability to loop through email). I'm aware there are a set of PHP IMAP functions located here: https://www.php.net/manual/en/book.imap.php

But my question is, does anybody know of an alternative libarary or IMAP wrapper class for receiving/viewing all your emails?

I seriously couldn't find anything, thanks in advance.

Lor answered 8/8, 2012 at 20:29 Comment(2)
I'm programming a utility, that's not going to help me. If I wanted to know about email clients that are available I would have posted on SuperUser...Lor
Try github.com/MonstaApps/PHP-IMAP-Fetcher. Pipe or fetch emails, log to MySQL, and save attachments.Stubbs
A
22

see below ur:--

http://www.php.net/mailparse

http://garrettstjohn.com/entry/reading-emails-with-php/

or try it:-

Reading Emails with PHP

<?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 = 'yourserver.com';
        private $user   = '[email protected]';
        private $pass   = 'yourpassword';
        private $port   = 143; // 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 = 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;
        }

    }

    ?>
Allative answered 8/8, 2012 at 20:44 Comment(1)
I got a Notice: Unknown: SECURITY PROBLEM: insecure server advertised AUTH=PLAIN (errflg=1) in Unknown on line 0 ??Ula
A
3

Probably the most robust would be using Zend's Email classes:

Current version

Zend 1.12 (no namespaces, good for legacy apps)

Ajaajaccio answered 14/5, 2014 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.