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.

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

<?php

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

    echo "<pre>";
    print_r($obj);
    echo "</pre>";

Where email.txt is:

Received: from outmail4.bc.edu (136.167.2.48) by BFRANKLIN04.bc.edu
 (192.168.1.34) 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
 [192.254.118.91])    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;
    h=to:subject:mime-version:from:content-type:content-transfer-encoding;
    s=smtpapi; bh=DvaYBQwmkNJzfT2Ef9kY09h415E=; b=Kg0JHzVv4X8KksSDVc
    0odRBz3RwI7oT1LxX1msR92Z1ilwpcUzGxMA/2Xu74RhzSraWJsjVipE2yuQ1TiM
    lljqwwxRgxTTYxMID3kV1Ql82k/e/G+iYwb+c9VB+43D7u51IYJR9ly/iKS6Pkur
    Cnk1SQEa1esHGuZTTe2yLSb3s=
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 [209.15.240.195])    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
Return-Path:
 bounces+836485-6dca-bc.person=bc.edu@outreach.nercomp.org

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()

Sources:

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

That last message was pointed to pritaeas

cereal already provided the code you need.

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

imap_fetchheader needs an IMAP connection, it can't read from textfiles.

Damn it, is there anyway then to pipe emails to a database?

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?

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:

<?php

    require './autoload.php';

    $server = new \Fetch\Server('pop.gmail.com', 995, 'pop3');
    $server->setFlag('novalidate-cert');
    $server->setFlag('ssl');

    # 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;
        print_r($message->getHeaders());
        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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.