How can I process incoming email messages with PHP? - php

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.

Related

How to call php file when we receive a mail

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!

Emailing content to a php script

I am about to start a project that requires an email to be somehow ran through a PHP script. I have full control of the server that the email will be sent to and wondered if people could give me some thoughts or pointers as to the most elegant way parsing it with PHP. I am not editing the email and then forwarding it on.
The server is CentOS with Exim email.
Thanks in advance.
You implement a client for the mail box (php imap modules can work with pop3, imap4 and local mailboxes). You poll the mailbox for new arrived messages, retrieve new ones and parse it. All using the php imap module.
One of the best examples of this comes with Wordpress... there is a file wp-mail.php which is set up as a cron task to retrieve and parse emails... I have hacked it up several times to do such things!
To make life easier for you, rather that stress yourself with parsing emails (if you don't mind the cost) you could decide to use Postmark
I've been using their services for quite some time and i love them. They have an Incoming email API service now. Enough talk, simply check it out because I believe it will help with what you're trying to do.
There are three main approaches to this:
Run a cron task to poll an IMAP/Pop3 server every x minutes
Make exim run a script whenever it receives an email
Use a third party service to receive the email and send it on to your site.
I wrote a Blog Post detailing the options, although it's for Rails the main concept applies to any language including PHP.

Getting emails into PHP web server

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).

How can I create a mail server?

I would like to have a server that is able to receive emails. Then I want to use PHP to program the way the emails are shown to the users. Can I do it purely with PHP? I mean, it is not a problem to send emails from PHP but I do not know if I can receive emails by PHP? (In a way PHP receives POST requests).
ADDED
As a response to the first answer, I would like to specify that it looks like I need an SMTP server. I want to be able to communicate with the SMTP server in a programmatic way. For example, I want to have a possibility to "tell" to the SMTP server to create a new e-mail address. I also need to know where incoming emails stored and in what format. For example, how I can extract the "sender", "cc", "bcc" from the file corresponding to the received mail.
would like to have a sever that is able to receive e-mails.
If you are writing it from scratch then you'll need the specification for SMTP. I would advise very strongly against this. SMTP servers are hard to write, and there are several really good open source solutions out there.
My understanding of PHP is that it does very poorly when it comes to multithreading, so it probably isn't a good solution for this problem.
Than I want to use PHP to program the way the mails are shown to the users
Servers that receive mails do not typically show them to users. They usually store them in a standard way (such as Maildir or mbox) which other software (such as a local email client or an IMAP server) accesses.
The job of showing email to a user is belongs to email clients. Web based PHP web mail software includes SquirrelMail and RoundCube. AFAIK they both act as IMAP clients. See the IMAP specification.
As a response to the first answer, I would like to specify that it looks like I need a SMTP servers. I want to be able to communicate with the SMTP server in a programmatic way. For example, I want to have a possibility to "tell" to the SMTP server to create a new e-mail address.
Pick an SMTP server that runs on your OS. Read the instructions to find out how to configure delivery and accepted addresses. It usually comes down to manipulating text files.
I also need to know where incoming mails stored and in what format. For example, how I can extract the "sender", "cc", "bcc" from the file corresponding to the received mail.
Again. See the manual for the mailserver. Most will give you options about where to store the data and in what format.
Then you just need to decide if you are going to get PHP to dig into those directly, or use an IMAP server in between.
No, that is not easily possible. PHP is made for (stateless) http protocol, while a mail is sent in a conversation that is built up from various requests and responses.
It is possible to parse and process mails using PHP, but I would recommend installing a mailbox that you can read from PHP using POP3. Then, your PHP application can show and process mails from that mailbox.

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

Categories