Mail to MySQL Database - php

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

Related

PHP Keep mails on server using POP3

Background of the project :
I am working on a project which goal is to keep track of all emails received in a mailbox.
The app connects to the mailbox and store every emails as files.
The user must have the possibility to use his mailbox regardless of the app behavior.
I am using the pop protocol at the moment i am writing this.
How it should work :
Let's say there are 10 unread mails in the mailbox, not stored by the app yet.
Case 1 : The user connects first : he reads, deletes others, tags them as spam or whatever : the app must still store each of the 10emails as files.
Case 2 : The app connects first, store the 10 emails and do something to never store them again, but the when the user will connect to his mailbox, he should still see the 10 emails as unread, and do whatever he wants with them.
How it works at the moment.. :
The function does retrieve every unread emails, and stores them as files (also downloads any attachments).
The first case works very well, i'm using outlook with my mailbox, and whatever i am doing with my emails, the app will still see the emails as unread, find them, and finally do the job.
The second case, however, seems to not work as intended : after storing the emails, i am using the imap_delete to mark these emails for deletion, and then, when everything is finished, i use imap_close with the CL_EXPUNGE flag to delete them, so that the app will not store them again next time...
The problem is that, this also deletes the emails from the server...meaning that in some cases, although the emails will have been stored, the user will never see them in his mailbox...
Question Time
Is there a way, in PHP, to delete emails only for my app, so it does not store the same emails several times, instead of deleting them from the server ?
I know outlook makes it possible, because if not, it would not work in the first case either...
Is there any solution to this ?
Is there a flag or a function that would do it correctly in php with the pop protocol ? Or is it possible using imap protocol and imap functions in php ?
Your problem is the IMAP protocol. While using IMAP you're always working with the mails stored at the server, so if you mark them as read, you're marking them read at the server so any further connections (using IMAP) won't see the unread messages, also if you delete them you're deleting it from server so no further visibility (either with POP - if never connected before, of course - or IMAP).
You should switch your connection protocol in order to use POP3, doing it so you download a copy of the mail to your machine (actually to the machine executing the PHP script) and work with a local copy.
See this question for more details.

How to send newsletters from different IPs?

I am working on a business marketing website. The criteria is that we have to send around 80,000 (say) newsletters per day to different email addresses. But my hosting company allowed me to send only 10,000 (say) emails from one IP per day. So to solve this issue my client provided me with 8 different IPs. I am having following questions:
I don't know how to send each and every newsletter from different IPs, means first from first IP, second from second IP and so on?
As I'm a PHP developer and not an expert at server end, can I accomplish this through my PHP script only or do I have to write a shell script to run at my server?
Is there any algorithm to accomplish such type of problem?
Had similiar problem couple years ago. I resosolved it in simple way. Create table in SQL, or create a class and serialize it. Class or SQL have to contain mails addresses (if You have some kind of volatile content then also mail content), do a class to send mail in certain order, and do object of this class on each ip. That depends what kind of config You have on server. I had a easy way cause all of IPs had its own folder with content, so I just put there code to do object, and just redirected couple of times the website, since it had to be done by web. If You have can have it by CLI there is pretty chance that You could do it by include or similar.
This way is pretty lame, I know it, but didn't want to do something more sophisticated at that time. Later on I wrote a class to manage mail connections via SMTP, so I could chose with mail from witch account would be send. That is better way, but not all servers could support it.
Another way is to do a cron job and do baskets of mail to send portions over the time. (this way was most common on servers that I was repairing).
Another way is to do a bouncing effect on servers with IP. Probably You could do also some shell scripting and invoke it via php script.
Well, I think that there are better ways, but it really depends on server config.
PS. Forgotten to mention, that code can be invoked by AJAX.

Email Piping System Setup

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.

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