Email Piping System Setup - php

Is there any tutorials out on the web that would show how to setup a program that would use piping, PHP and MySQL to setup email where replies would be sent into the database, or something similar to this?
Thank you in advance for any direction in finding out more about this.

PHP and MySQL are both mutually exclusive, and even more so from an email server.
To use PHP to receive email and insert it to a MySQL Database, you would have to write PHP code that is going to connect to a POP3/IMAP box and receive email directly and read new emails, subsequently inserting them into the database for handling them as you wish.
Luckily for you there are a whole host of mail related extensions for PHP. Note that you will specifically want to look at the set of imap_* methods (despite the name, they also work for POP3, etc).
I know, you could write your own SMTP server in PHP but who on earth wants to do that?

You can write own script which will check (via IMAP or POP3) for new e-mails in e-mail box and will store some e-mails directly into database.
Writing PHP CLI scripts is easy but there are some differencies between operation systems. See http://www.php.net/manual/en/features.commandline.introduction.php
The script can be periodically triggered via CRON or any other similar mechanism.

Related

A way to catching incoming emails [duplicate]

I'm building a photo sharing website (just testing PHP stuff) and I want users to be able to submit photographs using email.
For instance, the website needs to run a script when the users sends an email with an image attachment and text in the body. The image would be uploaded to the server, and a new "photo post" would be created with the text in the email body being the description.
My question is, how do I tell my server to run a script automatically when email is RECEIVED?
Any/all help is greatly appreciated. If you would like more info, just comment!
Thanks! -Giles
If you use cpanel you can pipe the email to a script which then processes the email accordingly. You can find that option under the email forwarders.
You want to use cron. It's the standard Unix way of running scripts on a regular basis without any user intervention. Create your script, make sure it can be run by the server user, then schedule it (the full command, e.g., php myscript.php in cron). It won't run when an email is received, but you can run the script often enough that the difference won't be noticeable.
If you are running the website on a UNIX server you have access to you can do this with procmail, sieve or similar mailtransport helpers. You would have to create a useraccount for the alias recepient as procmail is only invoked to process "real" users mail. Your .procmailrc would look something like this:
:0
*
| /usr/bin/php /path/to/your/script.php
And remember that procmail will pass it's info as arguments (and in the env variables).
The above scenario might not at all be possible for you, but if it is then I recommend taking a closer look at http://www.procmail.org/
Alternatively, you may be able to "pipe" the email directly to your PHP script. The process to do so will slightly differ depending on your email suite and/or server control panel software. You may be able to get "inspiration" from the Kayako manual at http://www.kayako.com/manuals/Kayako_SupportSuite_User_Manual_PDF.pdf (page 61 onwards) which shows how to setup email piping to the Kayako support helpdesk. You'll have to write the PHP file which reads in the file from STDIN yourself though.

Email website to post into database [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I receive email and process it in a web application
I simply was just thinking and I thought, wouldn't it be cool if my users could email us their posts and the server will post it for them.
So, John Smith might email "Wassup everyone" to post#mysite.com and it would post it for him?
Is there some kind of service that will do this, certain php function?
I'm just simply enquiring...
"One way to process the incoming messages is to configure the local mail server to start a PHP script when a message arrives on a given mailbox.
To achieve this, you need to learn specific details of how to configure your mail server because each mail server works in a different way.
For instance, if you use qmail mail server, you need to setup a .qmail file associated to the e-mail address that you want to handle. The .qmail file must contain the command that will be executed to start the PHP script that will handle the incoming messages.
...
An alternative solution to process received e-mail messages using PHP is to associate the incoming addresses to mailboxes accessible using POP3 or IMAP client scripts.
In this case, the messages are received and stored by the mail server, so they can be processed later by applications.
PHP scripts can use existing POP3 or IMAP client classes or extensions to regularly poll the mail server and retrieve the messages to perform the necessary processing tasks.
To perform this periodic poll, you can use the PHP CLI version command to start a PHP script by adding a task to cron on Linux and other Unix like systems, or the task scheduler on Windows.
Depending on how important the incoming messages may be, you may adjust the frequency of execution of the mailbox polling script."
http://www.phpclasses.org/blog/package/2/post/1-Process-incoming-email-messages-using-PHP.html
Not a php function, but there are libs that allow you easily reading email from server-side. The latest I saw was in Apache zetaComponents http://incubator.apache.org/zetacomponents/ . But I would prefer something more real than PHP for acting as a daemon, reading mail in every N timeframe and putting contents to DB.

Mail to MySQL Database

I want to send data to an emailaddress and save it into a MySQL database (PHP).
What is the easiest way to do this? (no form post, no curl etc., just email)
there are a few ways.
use a postfix alias script, basically, postfix receives your mail, and then runs it through a script of your choosing. The contents of the mail appear in STDIN - then you can do as you please with the contents of the mail.
Use a custom mail server like apache james, that is designed to process mail, and 'do something with it'
{A good one I found recently was in the google labs - http://freshmeat.net/projects/subethasmtp} - looked really good.
pls bear in mind that email is not a guaranteed protocol, if you are trying to do app-to-app messaging, then there are probably better ways!
Hope this helps., ace
If your server doesn't have imap but you have an outside system which does have cron you can write a page that checks the email account and loads the data into MySQL then set up a script on a machine that does have cron to wget the mysql page once every X minutes where X is how frequently you want to load data.
Alternately if you know the emails will load quickly you can set up small one field table in your DB with a time stamp. Each time your PHP script runs it checks the time stamp and if it's more than 5 minutes old it calls the email loading script before loading the web page.
If you can access the email using POP just have a process checking the email with POP functions and sending everything to your database
Also you could use php IMAP support
Assuming you know how to save the data to mysql already, you will need access to a mail server, either your own, a webhosts, or you could use a free one such as Gmail, or Hotmail.
You can then use PHP's IMAP functions (which can access POP too) to access the mail. Articles which may help you with this: PHP imap info and a tutorial on creating PHP webmail
There is a slicehost step-by-step tutorial that takes a unix based machine, installs postfix on it, sets it up to use MySQL and configures it to accept virtual users and serve/receive email from multiple domains.
The set up is clean, fast and secure. Life is good, eh?
http://articles.slicehost.com/email
Perhaps you should look at www.dbmail.org

Can a PHP app run when email is sent?

Is there a way to configure a server or a script to execute a php script when an email is received?
In theory this could be extended to other protocols, such as XMPP or SMS, etc.
What I have in mind is a user could send a message to checking-in#example.org and this would trigger a script which would then do whatever needed to be done, either irrelevant to the message (an automated message that gets sent whenever some other even occurs, like a server having issues) or related to the message (like it could store the subject in a database that other users could view as an RSS feed).
I know that most list-serve software have a means of sending commands (like unsubscribe), but I'm not sure how complicated the process is and if it is feasible to have something like this on a server-script level.
Would this need to occur at the IMAP/SMTP level, or could it be done closer to the script or HTTP server?
Just to give some context, one of the things I'm considering is a message-based clock in server for one of my work sites. Users could IM/email/text that they are at their scheduled location and this could trigger a script that would update a DB, rather then send the managers a direct message they need to log. This would just be one option (a web-based method is already in the works), but we want to make it as platform/protocol independent as possible. But if we can achieve that, we can look at implementing it for many other day-to-day needs.
Another way of asking might be: is there a way to have "discovery" of users from a server-script app or does something need to be doing a constant check to relevant sources to see such changes?
If you control your own machine you can configure it to pipe the e-mail through a filter. Simplest would be to setup a new account and setup a .forward or alias
The following examples are for Sendmail, but (all?) Unix e-mail programs offer a similar service.
Setting up an alias (option 1)
Look in the directory /etc on your server for your alias file. Add the line:
script: "|/path/toyourscript/pipe.php"
Using a .forward file (option 2)
Create a .forward file in your main home directory.
"|/path/toyourscript/pipe.php"
or:
myemail#example.com,"|/path/toyourscript/pipe.php"
If you are on shared hosting then most hosting providers also provide the possiblity to "pipe" e-mails received to a particular account through a script instead of storing them in a mailbox. Check the CPanel setup.
Use a cron job to check for emails through a pop3 interface. If an "interesting" mail is found, run your php script.
Note that the cron script can be in PHP too.
As jldupont said, it is easy to do ith php itself, simple reading the smtp continuosly with a cronjob.
Otherwise, you could use a daemon, in python for example.
Both ways allow you to do an important thing: check if the sender of the email is a your user (you have the users in a db, right?), or a spambot (nomatter what kind of anti-spam you use, soon or later, checking-in#example.org will be full of spam)

How to delay the dispatch of an e-mail? Click button to send NOW but wait until 6.00pm until the email gets actually sent

I have a php script that does some processing (creates remittance advice PDFs, self-billing invoices, Sage CSV file etc...) and at the end outputs a screen with a form, in which the names and e-mail addresses of the people paid appear. User makes a selection of names by clicking check boxes and then there is a Send button which sends out emails with remittance advices and self-billing invoices attached. This all works nicely, BUT they now decided that when they click the Send button, they would like the e-mails to be sent NOT straight-away, but at 6.00pm.
Is it possible to set the dispatch time of the message in the SMTP header? Can the MS Exchange server be configured so that e-mails from a particular sender will be held until a certain time before they get sent? IT Support dept. claim it used to be possible in the old days of dial-up connections when it was simply cheaper to send stuff at night... but that this functionality was removed. Is this true? I have no idea how hard is the task at hand. It seems very straightforward and I guess it really is a task for the IT support guys to handle. But maybe I am wrong?
If this can not be set up at the Exchange server side, how could I go about achieving the requested functionality? And, no, this is not an exact duplicate of this question. I had a look at that but it didn't seem to answer my questions. Any help greatly appreciated!
Edit
Apache running on MS Windows Server 2003. Database is Oracle 10g. There will be no CRON set up. The email queue table would need to store all attachments too. I would like to avoid doing this at all cost. No way to specify dispatch time in header?
You could save those emails into a database. And then create a cronjob PHP file that executes every few minutes to check if there are emails to be sent in the "queue" database.
Here's a tutorial on something like this.
Windows (non-server editions, at least) has a "Scheduled Tasks" control panel item similar to CRON. Just from looking at it briefly, you can probably write a PHP script which sends your mail "now," but run it using the CLI at whatever time you want to send the mail using Scheduled Tasks.
Actually you can do this. If you are using an exchange 2003 server, you can set up a different SMTP connector. Under the delivery options there is a way to change it from send always any scheudle you pick.
Here is a nice tutorial for setting one up.
link text
One thing you will need to do is when you create the new SMTP connector, set it up with a different port to listen on. That way you can send to that one instead of the standard connector.

Categories