Hello Community,
I was wondering if there is a way to filter through an email to get all the details. Such as from, to, subject, body and a few other details but you get the idea.

At the moment I just have a bunch of text but I want to get all the little bits of information.

2 Years
Discussion Span
Last Post by joshl_1995
Featured Replies

In addition, as example you can try imap_rfc822_parse_headers():


    $header = file_get_contents('email.txt');
    $obj = imap_rfc822_parse_headers($header);

    echo "<pre>";
    echo "</pre>";

Where email.txt is:

Received: from outmail4.bc.edu ( by BFRANKLIN04.bc.edu
 ( with Microsoft SMTP Server id 14.2.347.0; Thu, 9 Jan 2014
 09:52:28 -0500
Received: from o1.outreach.nercomp.org (o1.outreach.nercomp.org
 [])    by krait.bc.edu (8.14.5/8.14.5) with SMTP id s09EqQix012162
    for <bc.person@bc.edu>; Thu, 9 Jan 2014 09:52:27 -0500
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=outreach.nercomp.org;
    s=smtpapi; bh=DvaYBQwmkNJzfT2Ef9kY09h415E=; b=Kg0JHzVv4X8KksSDVc
Received: by mf253.sendgrid.net with SMTP id mf253.19201.52CEB7AA5        Thu,
 09 Jan 2014 14:52:26 +0000 (UTC)
Received: from prod3.jjcbigideas.com (unknown [])    by mi24 (SG)
 with ESMTP id 143777d7221.2da7.f27c    for <bc.person@bc.edu>; Thu, 09
 Jan 2014 08:52:26 -0600 (CST)
Received: from nercomp by prod3.jjcbigideas.com with local (Exim 4.82)
    (envelope-from <nercomp@nercomp.org>)    id 1W1GyA-0008Uc-2Q    for
 bc.person@bc.edu; Thu, 09 Jan 2014 08:52:26 -0600
To: <bc.person@bc.edu>
Subject: NERCOMP - Security Training and Risk Assessment - Feb 10
MIME-Version: 1.0
From: <events@nercomp.org>
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable
Message-ID: <E1W1GyA-0008Uc-2Q@prod3.jjcbigideas.com>
Date: Thu, 9 Jan 2014 08:52:26 -0600

The example header will return an object array:

stdClass Object
    [date] => Thu, 9 Jan 2014 08:52:26 -0600
    [Date] => Thu, 9 Jan 2014 08:52:26 -0600
    [subject] => NERCOMP - Security Training and Risk Assessment - Feb 10
    [Subject] => NERCOMP - Security Training and Risk Assessment - Feb 10
    [message_id] => 
    [toaddress] => bc.person@bc.edu
    [to] => Array
            [0] => stdClass Object
                    [mailbox] => bc.person
                    [host] => bc.edu


    [fromaddress] => events@nercomp.org
    [from] => Array
            [0] => stdClass Object
                    [mailbox] => events
                    [host] => nercomp.org


    [reply_toaddress] => events@nercomp.org
    [reply_to] => Array
            [0] => stdClass Object
                    [mailbox] => events
                    [host] => nercomp.org


    [senderaddress] => events@nercomp.org
    [sender] => Array
            [0] => stdClass Object
                    [mailbox] => events
                    [host] => nercomp.org



In order to use this function you need to enable the IMAP extension, under debian & co. you can run:

sudo apt-get install php5-imap
sudo service apache2 reload

The reload is needed, otherwise the server will not read the new configuration.

To create something more complete and to fetch also the body you have to use the other functions:

You can also try Fetch which is a library based on the IMAP extension:

You can access the headers and the body by using the methods getHeaders() and getBody()


Edited by cereal


Okay but how do I go about doing this?
All I reall want is the Subject, From, To and Body (Plain text).


I know, when I typed that they posted that reply. So I ment to send this before they posted that.

Also how do I use imap_fetchheader ? Just like the example cereal posted


Pipe from where? Your files, or from the server? In case of the latter: IMAP is not available on your mail server?


I want it to pipe emails to a PHP script (which I've got working but now need to read the data). So when someone sends an email to this email address it will be forwarded to the PHP script and will filter out all the data I want an insert it into the database.


Maybe I didn't understood your initial request: you have a form to send an email, let's say with the mail() function, and you want also to save the input to the database, correct? Or you want to access the inbox of an email account to read the headers and the body of the received messages?

Edited by cereal


No I want the received email inserted into the database.

Whenever someone sends an email to my websites email address it is piped to a PHP script, I then want the information (that was piped) filtered to get who it was sent from who it's sent to, subject and body which I will then insert into the database.


Whenever someone sends an email to my websites email address it is piped to a PHP script

Ok, but I hope I can make you understand that when an email message is received by your website, in practice is received by the mail server associated with your domain name.

This does not involve the web server (Apache for example) and neither the PHP engine. So you cannot redirect directly what is received by the mail server to a PHP script.

You can only access the email account and parse the received messages.

Imap, if supported by your mail server, is a protocol that allows to easily access email accounts, otherwise you can use POP3, which is supported also by the Fetch library suggested in the previous post, as example of connection, it could start with two or three arguments:

# for imap
$server = \Fetch\Server('imap.domain.tld', 993);

# for pop3
$server = \Fetch\Server('pop3.domain.tld', 995, 'pop3');

By default the third argument is imap so it can be avoided in the first example. It is just like a connection string to a database. The second argument is the port, and the first is the server path.

If, for example, you want to parse the mails from your GMail account you would do:


    require './autoload.php';

    $server = new \Fetch\Server('pop.gmail.com', 995, 'pop3');

    # set username and password
    $server->setAuthentication('*****@gmail.com', '*******');

    # get 3 messages
    $messages = $server->getMessages(3);

    foreach ($messages as $message)
        echo "Subject: " . $message->getSubject() . PHP_EOL;
        echo "Header: ".PHP_EOL;
        echo PHP_EOL;

In the case of the GMail example you will need an application password and to enable the IMAP and/or POP3 access. But a part these peculiarities, most of the procedure is the same for all the hosts, just check the Server.php class to see the options for the connection and the Message.php class to retrieve the information from each email message.


Thanks but I pretty much need immediate email action so once an email comes in I need it placed in the database, that is why I like the piping system but sadly email headers couldn't be anymore cluttered with crap so it's hard to filter out information so I guess this is my only option.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.