Email queue

for osCommerce Online Merchant v2.2

Email queue. one of the problems I faced was that on several occasions the response of the system was rather slow. The main cause it seems was the email sending via smtp of the isp provider. Checkout confirmation was one of those occasions especially if you add the additional email to the store owner. Therefore, not being able to speed up the smtp, I decided to separate the email processing from the online transaction. In short, I do not send the emails at that time but create them as before and store them in an email queue to be processed later via a batch job. That job can run hourly or every minute. The main benefit is that the online transaction and thus the site response does no longer have to wait for the php mail function to complete. to do this the following: 1) add this new function to general.php : function tep_store_mail($to_name, $to_email_address, $email_subject, $email_text) { $sql_data_array = array('to_name' => $to_name, 'charset' => CHARSET, 'to_email_address' => $to_email_address, 'email_subject' => $email_subject, 'email_text' => $email_text); tep_db_perform('email_batch', $sql_data_array); } 2) add the new table to your database : # # Table structure for table 'email_batch' # DROP TABLE IF EXISTS email_batch; CREATE TABLE email_batch ( email_id int(5) unsigned NOT NULL auto_increment, charset varchar(20) default NULL, send char(1) default 'N', to_name varchar(50) NOT NULL default '', to_email_address varchar(50) NOT NULL default '', email_subject varchar(100) NOT NULL default '', email_text text NOT NULL, last_updated datetime default NULL, PRIMARY KEY (email_id), UNIQUE KEY email_id (email_id), KEY email_id_2 (email_id), KEY send (send) ) TYPE=MyISAM COMMENT='batch emails'; 3) create the batch php job which sends the emails out email_batch_send.php this job selects all emails not yet send, sends them and sets them to send. You may also opt to delete them, I do not so I do not need the extra email to the store owner as I can look in the database. I do need to make an admin management screen for this, unless there are vulanteers. Another benefit, you can send them again and again and... schedule this php job at your convenience. <?php require('includes/configure.php'); require(DIR_WS_INCLUDES . 'filenames.php'); require(DIR_WS_INCLUDES . 'database_tables.php'); require(DIR_WS_FUNCTIONS . 'database.php'); tep_db_connect() or die('We are currently unavailable due to Maintenance..'); require ('includes/configuration_cache_read.php'); require(DIR_WS_FUNCTIONS . 'general.php'); require(DIR_WS_CLASSES . 'mime.php'); require(DIR_WS_CLASSES . 'email.php'); $email_query = tep_db_query("select * from email_batch where send = 'N' "); while ($email = mysql_fetch_array($email_query)) { define('CHARSET', $email['charset']); $email['email_text'] = str_replace("n", '<br>', $email['email_text']); // not sure why but I need this for html tep_mail( $email['to_name'], $email['to_email_address'], $email['email_subject'], $email['email_text'], 'Crystal Light Centrum', ''); tep_db_query("update email_batch set send = 'Y', last_updated = now() where email_id = '" . $email['email_id']. "'"); } mysql_free_result($email_query); ?> 4) everywhere where the tep_mail function is called, change that function to tep_store_mail. you can leve the parameters the same, only change the name. 5) in case of password forgotten, you may opt to keep sending the email immediately ofcourse.