|
Location: Web development - PHP License: The Common Public License Version 1.0 (CPL) Mail queueing using PEAR Mail_QueuePosted by xsist10Creating a mime mailing queue using PEAR's Mail_Queue library |
Skill: IntermediatePosted: 20/11/2008Views: 2731Rating: 5.00 /5Popularity: 0.00 |
| Sign Up to vote for this article |
Most of us at some point have needed to add some form of mailing functionality to a website/application. However as the demand on the system has increased we've had to send more and more emails (billing runs, newsletters, etc) to the point where 1 page execution has been insufficient to send a few thousand emails.
Thankfully we have a simple solution provided by the PEAR libraries, Mail_Queue.
You'll need to get hold of the PEAR Webservice library first (and the core PEAR libraries if you don't have that already). You can follow the installation instructions to get PEAR installed on your system at:
http://pear.php.net/manual/en/installation.php
The package we will be using can be located here:
http://pear.php.net/package/Mail_Queue/
For Ubuntu users you can install PEAR, DB (to handle database connections) and Mail_Queue like this
$ sudo apt-get install php-pear
$ sudo pear install -a DB
$ sudo pear install -a Mail_Queue
You may have to install some dependants of the Mail_Queue like Mail_mime (check the install message from Mail_Queue for these dependants). The -a flag should take care of them. If not then install them manually.
Mail_Queue uses a database tableto store queued emails so we'll need to create that first. The database design can be found here: http://pear.php.net/manual/en/package.mail.mail-queue.mail-queue.tutorial.php and looks like this.
CREATE TABLE mail_queue (
id bigint(20) NOT NULL default '0',
create_time datetime NOT NULL default '0000-00-00 00:00:00',
time_to_send datetime NOT NULL default '0000-00-00 00:00:00',
sent_time datetime default NULL,
id_user bigint(20) NOT NULL default '0',
ip varchar(20) NOT NULL default 'unknown',
sender varchar(50) NOT NULL default '',
recipient text NOT NULL,
headers text NOT NULL,
body longtext NOT NULL,
try_sent tinyint(4) NOT NULL default '0',
delete_after_send tinyint(1) NOT NULL default '1',
PRIMARY KEY (id),
KEY id (id),
KEY time_to_send (time_to_send),
KEY id_user (id_user)
);
You'll now need a scheduled event to send your queued emails. We'll save this script as queue.php.
You'll then, if you're using Linux, have to add the script to your cronjob file. This is normally located at /etc/crontab.
You'll have to add a new entry that looks something like this:
*/5 * * * * user /usr/bin/php -f /path/to/script/queue.php
What this basically means is that every 5 minutes our script will run and send 50 emails (10 attempts for each) from the top of the queue to their intended recipients.
That's a basic mail queueing system done. There are various modifications you can make by using different features of the Mail_Queue library or you can hack the Mail_Queue library yourself to include other features (like mail priority, mail delaying, etc).
I personally use this library in a professional environment to mail thousands of invoices with PDF attachments every month without problems.
Points to note are:
And that's it. A simple but highly useful library for handling your PHP application's emails.
This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)
| xsist10
| Location: |
Sign up to post message on the article message board!