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