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 ticketing 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.
Related
I have special e-mail accounts on my web server (#mydomain.com). I want to run a php script automatically when one of these accounts get a new e-mail. For example: when info#mydomain.com address receives a mail, I want to run "receivedMail.php" file and read this new e-mail. I don't know where I will start or how can I do this.
You're wanting to pipe email received for a specific email address to a PHP script.
If your webhost has cPanel, this makes it very easy to setup. See:
http://kb.siteground.com/how_to_pipe_an_email_to_a_php_script/
Alternatively, if you don't have cPanel but have Exim mailserver, this will guide you on how to do this:
http://www.phpshare.org/articles/Piping-Incoming-Mail-with-PHP
Hope this helps!
You have to create a cron job calling your php file every 1 minute for exemple.
The script have to the derver and read the email.
This has nothing to do with web servers directly, Since web servers do not receive emails. So no .htaccess style files come into play. The email is received by a mail server, so that is where you have to get active. Two strategies are possible:
you use the possibilities to trigger an action as offered by your
mail server (typically the smtp server you operate). That obviously
depends on which mail server you operate, different software offers
different features.
you poll those email accounts on a regular base, using a standard protocol like POP3 or IMAP4. You can do this using any suitable client. When a new message is found whilst polling the account, then you trigger the action you wish.
Option 2. is probably easier to start with. So give it a try: create a php file which polls your email server. You can use the imap php extion for this, it supports all important email protocols. The extensions allows to easily detect and retrieve new messages. For each such message you can implement whatever action you wish. All that is left is to run this script on a regular base, say every 3 minutes, which is typically done using a cron system. There are many examples for this out there on google...
Have fun!
I want to process incoming emails to an address and insert them into a mysql database. I am using a grid-service media temple host with SHH access but I have no idea where to start. I also want to store attachments or copy them to a directory on my host and save the url in the database.
Why not pipe the emails instead of using imap_open()? This would allow you to access the email immediately as it comes in, and then decide if you even want it stored in the inbox.
The simplest way to process incoming mail in PHP is imap_open(), which you can use to open a mailbox, either using IMAP4 or POP3.
You would need to run your script periodically, or at least regularly run imap_ping() to 'poll' for new messages.
Since you say you don't know where to start, my one suggestion would be to look at the inbound email service offered at Postmark. I believe there are several such services.
I want an easy way to get my email (using Mac Mail or Outlook) to a web server using Apache/PHP to be processed and added to an archive, a database. Is there an easy way to do this? Right now, it seems my best option is to save to disk, open up a page on the site, and upload the email like a file. Is there a better way?
If you want to do it as one off, saving and uploading will probably be the easiest solution.
If you want to do it continually, you might want to look into piping email into php Email piping with php script
Alternatively you can check an IMAP/POP3 mailbox from PHP.
You could create a cron script to automatically scan a specific directory on your webserver daily (or even hourly) to load the files into a db. You would still have to copy them to the webserver manually.
I'm sure there is a better way than that though.
If your aim is ultimately to archive your mail store and even better have your mail flowing to the archive in real-time (e.g once the mails are older than a certain age) then this tool will support your needs. Outlook Mac Archive Tool! calls it Cloud Archive and allows for mail to be stored in mail clouds like Gmail.
The advantage of archiving to a folder in a GMail account is your mail is always accessible through a web interface, searchable and safely backed up in free storage. You could use multiple Gmail accounts if you have excessively large mail stores (e.g. one for each year of mail).
Lets take a look at the following scenario:
I have a websitesite, site.com. One customer support tech guy has an email account on it: techguy#site.com. Customers send messages to that email, tech guy replies via that email. They are both using desktop email clients (thunderbird, outlook...).
What I know is: using php to read incoming mail from customers
What I want to know is: how to use php to read outgoing mail that the tech guy is sending from his client via smtp, imap..
This would be used for archiving conversations between techs and customers without forcing them to use some php app on the site to send emails (I want them to use desktop clients).
I dont know much about email protocols, but I have plenty of experience in php. Im guessing that the email daemon could be setup to call a php script every time it recieves an outgoing email or something like that. Is there any way that this can be done?
The way how some products on the market are doing it is by requiring the user to install a plugin on his/her email client. Using this method doesn't matter what smtp server is used, the mails will be logged using the plugin.
Another way(this is just an idea) is to "force" the tech guy to use your smtp server so you can access the logs and see who sent mails and who is the receiver(accessing the logs using php)
It would be much easier to have a separate email address for logging purposes and simply keep it in the cc or bcc field for all communication between customer support and clients.
since you can have php interpret the emails, the best thing to do is have a dedicated email address that gets piped to a script (i know this is easy to set up cpanel)
you have the client email support#domain.com which gets piped to the script - do what you want with it there.
then you have the script email back out to bob#domain.com with a from address as support-reply#domain.com - which is also piped to a script
when he replies to that, it goes to support-reply#domain.com which goes to a script, which in turn you would process, then forward back out to the client#google.com with a return address of support#domain.com
i think amazon uses a similar technique so that people can email each other back and forth, but they create coded email addresses and have a database to keep track of who they are going to and coming from.
I know you can send emails with PHP but, can you receive emails through PHP?
You can pipe incoming mails into a PHP script so you can process it directly. Evolt has an article on how to setup something like that. This can be useful if you want to activate scripts programmatically by sending Emails to it (like responses to a newsletter mail that unsubscribes the user).
If you just want to read mails using PHP, PHP has native functions to talk to IMAP, NNTP and POP mailboxes.
Take a look at http://cloudmailin.com it takes away a lot of the hassle involved with receiving the email and will send it directly to your app via an HTTP Post. We have quite a few php users using the system to receive email.
You could write a mail server in PHP that binds to a port and listens for incoming email.
But PHP is not the language I would recommend for tasks like this, and it would also be a hugely complex undertaking.
You can hook into an existing mail server as callback script, or periodically query a mail server via POP or IMAP. The latter option is the most common: run a PHP script that processes an email account via a cron task in intervals. See http://php.net/imap.
There is a great library that is based on IMAP extension: http://code.google.com/p/php-imap
Only a mail server can receive e-mails. You could read mail box formats (such as mbox or Maildir) to read e-mail using PHP.
PHP scripts functioning as IMAP/POP3 servers can receive e-mail sent to them.