Is it possible to write an email parser in PHP?

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 concerts to their profile page. I would like them to be able to send these updates by email, as I can add tasks to my Todo online list.

So, I need to parse the emails that get to my server account.

I believe that this is true to provide a standard format .. And I thought

(In subject)NEWGIG [[or maybe set up a separate " newgig@mySite.com " 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 

A concert will be added and a confirmation email will be sent.

How hard is this to do? Is it possible? I think I can do all the parsing and SQL, etc., but I don’t have much experience with mail, and it seems that there are a lot of “awkward bits” to watch out for.

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

BTW - I deal with the parser by email because I want it to be very easy to use. Musicians, as you know, are lazy, so if I can let them publish the concert directly from their email (where they usually receive confirmation), this saves them from having to visit the site, log in, go to their account, etc. .d.

+4
source share
3 answers

Yes, it is doable. All you need is access to the server with the smtp endpoint. something like postfix or exim or something else that listens for incoming email. you need to set up this software to send incoming text in plain text to a script or your program that can handle input from stdin .

The easiest way is to create a new email alias that points to a script. I have many entries in the /etc/aliases file that look like this:

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

this works whenever an email arrives at this address: userpart@this.server.example

the script itself reads input like this

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

you may find using the existing MIME Parser useful, as input can be something like modern MUA.

+7
source

Parsing letters is like parsing any data. You just need to define a common format for the letters.

After you have written the parser, create a script that periodically connects to the mail service using the php imap functions, reads letters, parses them, and inserts them into db.

But it would be easier and faster just to create a web form in which they will add all their concert data.

+2
source

You can use regex for this. But you need some improvement in your format.

Make sure your format does not contain characters with which the user can enter it and destroy your format. If you want to make this very easy for your users / clients, use the XML format. It will be easier for users to edit it.

Here is an example: (I create a way to get TITLE , and I change the open and end tag). Now it uses double curly braces .

 $str= ' (In subject)NEWGIG [[or maybe set up a separate " newgig@mySite.com " 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'; preg_match('#{{TITLE}}(?P<title>.+){{TITLE}}#', $str, $matches); echo $matches['title']; 

XML format (you can use the simplexml_load_string function to parse the XML format):

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

Source: https://habr.com/ru/post/1435273/


All Articles