Mimic ticket system email-to-post functionality
What is the best way to parse email contents? For example, we want to be able to parse emails that are sent from 3rd parties and put them into a database. Right now we use Google Hosted accounts and we were thinking about maybe using PHP IMAP functionality to pull emails every couple minutes and parse their contents, putting it into our DB.
Wondering if that is overkill? If we sent the emails to a dedicated server would there be a way to grab and parse them with PHP?
Not sure how ticketi开发者_运维问答ng systems do it... put they allow you to create a ticket by sending an email to a specified address.
If you send the e-mails to a server, you can actually just set your forward file to pipe the e-mail to your parsing script by placing the following line in the .forward
file of the e-mail account on the server:
| php createticketfromemail.php
That way you don't have to periodically poll, whenever an email arrives it will be piped to your script.
http://www.softpanorama.org/Mail/pipes_in_dot_forward_file.shtml
EDIT
To address the point that @miemos brought up in his answer. You can structure your forward file to both store a copy of the email in your inbox and pipe it to the script, that way if the script fails you will still have a backup copy of the email somewhere.
The safest way to receive and process messages using PHP is using a POP3 or IMAP mailbox. You can poll the mailbox regularly and process the messages and delete them afterwards.
Some systems allow you to pipe a message to a PHP script when the message arrives. This is not a safe way to process the messages because if for some reason your script fails you loose the message forever. Using a mailbox is safer because you can delete the messages after they are successfully processed.
精彩评论