Forum Moderators: coopster
(N.B. Sorry this is long, but I wanted to give you as much info as possible.)
I have very limited knowledge of servers and the core workings of PHP, so go easy on me.
I am currently building a HTML emailer module for a control panel we have. Now, we have had these before, just not built properly.
Before, we have sent out to a maximum of about 2000 people, so nothing server stressing. However, next week, we need to send out a HTML email to ~250,000 people (joy). So, I am looking into the best way to send out this email.
Current Method
At the moment, we send emails using an AJAX system. A user selects their email, mailing list(s), settings, etc, and clicks "Send". They are then taken to a "Sending" page with a progress bar and a PHP file is called through AJAX.
This file then sends about ~100 emails before terminating (to ensure it keeps within max execution times). It publishes it's results back to the parent via the AJAX connection and the progress bar is updated. If it is not 100% sent, the file is recalled via AJAX and the next ~100 are sent, and so fourth.
Now, I know this is a very bad way to do this. If the user closes the page, the send is aborted, etc.
Desired Method
This is how I think I should be doing it, and please correct me if this method is bad.
It begins the same, the user chooses their settings, and they click Send. Upon which, the settings are saved to a MySQL DB, and a PHP file is then set running in the background of the server. The user is then taken to a success page saying "The email is being sent to the mailing list..." or something.
Now, this PHP file being run in the server background would get it's information from the MySQL DB using argument passed to it. It would then send a set number of emails to allow for maximum execution time. Once the set number is sent, it would update the MySQL DB.
If the send is not complete (aka more need to be sent), it will run itself again in the BG, and so fourth until the send is complete.
The user can monitor the progress in our control panel using the information in the MySQL database.
Now, 3 questions,
1) Is this doable?
2) Should I do it this way?
3) How do I run this PHP file in the BG and how do I make it call itself again?
Sorry if I am asking a lot, but I am new and fast learner. So, go step by step if you wish.
Thank you!
I would set up the PHP to feed a queue file, then have the system toolkit do a cron task and read the queue.
There's also a good chance that the mail system on your server may already be set up to do this, so you may just need to write some files to a queue directory.
However, notifying the user of errors becomes problematic in these cases. I would actually have a progress page that discourages the user from closing or quitting until things are complete.
[edited by: cmarshall at 1:19 pm (utc) on Feb. 7, 2007]
Assuming you are hosted on a unix/apache server, and assuming you have access to some form of control panel for your domain/databases, search for the Crons.
In a cron, you specify one file and how often it will be executed by the server (very simple description).
The file, you already have (just copy/rename and modify it so as not to print anything on screen - it must appear as a blank white page). Leave all the rest of its functions intact (this means the sending procedure).
If you must, create a new file that sends 100 emails (unflagged) and update the database with the "send" flag.
In your domain contrl panel, in cron options, set the file to be executed every 10 minutes, every day.
If all are set correctly, you will have 1 script that send 100 emails each time it is executed. This script will be executed by the server every 10 minutes, every day (no matter if any email is actually send).
Hope this helps and makes sense.
We have a Unix/Apache webserver running CentOS. Our nice hosting providers also have Plesk on there so running a contrab is easy (as far as I am aware).
I am probably going to go down this route and most people are pointing me this way anyway.
Now, can you edit the frequency of the contrab dynamically using PHP? If not, no biggy really, just curious as I could add a bit more functionality.
Also, some final queries about running a PHP file in contrab. Is it pretty much the same as running a PHP file normally except you must not pump anything out? More accurately, can you include files? etc.
Thanks.
exit;if there is nothing in the queue. I have a PHP script that I run via cron that processes a mail queue DB in MySQL. Even if your script doesn't produce any output, it will still produce headers. Call PHP with the -q flag to suppress header output:
/usr/bin/php -q /absolute/path/to/mailer.php