Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write an email parser in PHP? [closed]

My PHP skills are intermediate, but I want to try to implement this:

I have a site for local jazz musicians. I would like them to be able to add upcoming gigs to their profile page. I would like them to be able to send these updates in by email, like I can add tasks to my online Todo list.

So I need to parse emails that land in my account on the server.

I think its fair to enforce a standard format . .and I was thinking

(In subject)NEWGIG   [[or maybe set up a separate "[email protected]" email account for gig updates only]]

TITLE>My next super gig<TITLE
DATE>03/10/2012<DATE
DESC>Here is some supplementary information<DESC
LINK>www.hereIsTheVenue.com<LINK

The gig would be added and a confirmation email sends out.

How difficult is this to do? Is it possible? I think I can do all of the test parsing and SQL, etc .. but I don't have much experience with mail and it seems like there are a lot of "fiddly bits" to watch out for.

Can anyone confirm that this is doable before I start? Any tips or things to look out for?

BTW - I'm doing the email parser because I want it to be super easy to use. Musicians are notoriously lazy so if I can let them post a gig right out of their email (where they are usually getting confirmation) then it saves them the hassle of going to the site, logging in, going to their account, etc.

like image 554
K.K. Smith Avatar asked Dec 26 '22 17:12

K.K. Smith


1 Answers

Yes, it is doable. All you need is access to a server with an smtp endpoint. something like postfix or exim or anything other that listens to incoming email. you need to configure this software to pass incoming mail as plaintext into a script or program of yours which can handle the input from stdin.

the easiest way is to create a new email alias which points to a script. i have plenty of entries in my /etc/aliases file which look like this:

userpart: "|/usr/bin/php -f /path/to/some/script.php"

this gets triggered whenever an email arrives on this address: [email protected]

the script itself reads the input like this

$fp = fopen('php://stdin','r');
    while (!feof($fp)) {
            $input .= fgets($fp);
    }
fclose($fp);

you might find the use of an existing MIME Parser useful since the input could be anything a modern MUA could think of.

like image 181
Jan Prieser Avatar answered Jan 12 '23 21:01

Jan Prieser