4

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 "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

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.

K.K. Smith
  • 990
  • 2
  • 9
  • 28
  • If you just want to send out an email with PHP (even if it has HTML) then it is very possible. – greduan Sep 20 '12 at 13:17
  • PHP is a fully Turing complete language. Yes, it's possible. This question would be more constructive if you'd specify problems you have in implementing this system. – deceze Sep 20 '12 at 13:18
  • Fair enough.. but I'm not sure how many hours I should spend trying to implement something I am not sure is possible, or may be vastly easier/or harder depending on a certain approach. So I come here for a bit of a "heads up". Fair, I think. – K.K. Smith Sep 20 '12 at 13:52

3 Answers3

7

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: userpart@this.server.example

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.

Jan Prieser
  • 1,529
  • 9
  • 15
  • This seems to be the most popular solution, so I'm sure it's great advice. My PHP level is such that I have no idea what postfix, exim and stdin are. In my head I saw this approach: 1. Run cron jobs to check my incoming mail directory 2. Read and parse new emails 3. Keep a list of mails that have been processed to avoid "doubling" Is this at all compatible with what you have said? I'll gladly go and do some homework. I' not even sure why I would need a MIME parser if I only need to parse the body of the mail? – K.K. Smith Sep 20 '12 at 14:09
  • your 3 steps are all unnecessary with my approach. the software thats putting the mail in your inbox is something like postfix or exim. A Mail Transport Agent (MTA). You can tell this software to call your php script directly upon arrival. all you need is a server with a FQDN. The MIME Parser is needed because you cannot force your customers to send plain text mails only. most of the mail clients send html mails by default with variying body parts. – Jan Prieser Sep 21 '12 at 08:25
  • and you need full root access to the system, too. its not working on shared hosting. but you didn't mention your environment in your question. – Jan Prieser Sep 21 '12 at 08:27
  • Hi @JanPrieser, can we do the same way to achieve this http://stackoverflow.com/questions/25448169/php-web-app-and-reply-to-email-to-call-a-function. – Rifky Aug 25 '14 at 05:10
  • @Rifky yes, this would be the same approach. – Jan Prieser Aug 27 '14 at 13:22
2

Parsing emails is just like parsing any kind of data. You just have to define a common format for the emails.

After you have written the parser, create a script that periodicaly connects to the mail service using php imap functions, read the mails, parse them and insert them into db.

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

Vlad Balmos
  • 3,372
  • 19
  • 34
  • This is the approach I saw in my head. Except cant I just access the mail directory as a directory? Why do I have to connect using IMAP functions? (My knowledge of the mechanics of emailing are obviously lacking) I agree the webform would be easier... but I really like the system of adding via email. It's how I add tasks on my task manager and I am just wondering how hard it is to implement. – K.K. Smith Sep 20 '12 at 14:12
  • 1
    @K.K.Smith it all depends on how your mail server works. If it supports pop3/imap protocol then you have to communicate with the mail server through that protocol(s). This is how email clients work, they talk to the mail servers and request mails from the inbox. You are essentialy trying to create a basic mail client. Or you could go the way Jan Prieser said in his answer. But it means you have some server side configuration to do. Not feasable on shared hosting – Vlad Balmos Sep 20 '12 at 14:16
  • Thanks for your help. Last question? I'm on Hostgator and above my public_html folder I have a "mail" folder with "sitename">"emailAccount">"tmp", "new", "cur". I assumed these would be where I would simply be using opendir() and readdir(). The fact that everyone mentions connecting to a server through a protocol makes me think I'm wrong in that? – K.K. Smith Sep 20 '12 at 14:40
  • @K.K.Smith I'm not familiar with that kind of setup so i can't say for sure, but my guess is that those directories are related to the system mail (sendmail) which uses files to store the actual mail sent between the local users of that server. You can ask your host if your mail is stored there and go from there – Vlad Balmos Sep 20 '12 at 18:27
  • @K.K.Smith one last thing to add, it's easyer to use the imap related functions to fetch mails and get the body then to actual parse an entire mail (headers, body) from scratch using php. If i understand correctly, you are reluctant to use new technology due to the familiarity of `opendir()` and `readdir()`. Don't be. Always try new things, that's how you will aquire more knowledge. This project of yours will teach you a lot more about how email works then you imagine :) Good luck! – Vlad Balmos Sep 20 '12 at 18:39
1

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

Make sure your format doesn't contain characters whom user can input it and destroy your format. If you want to make it very simple for your users / client use XML format. It will be more easily for users to edit it.

Here is the ex: ( I create how to get TITLE and I modify the opening and ending 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 simplexml_load_string function to parse 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>
Joko Wandiro
  • 1,957
  • 1
  • 18
  • 28