237 lines
9.3 KiB
PHP
237 lines
9.3 KiB
PHP
<?php
|
|
|
|
//
|
|
// Quick Send Function
|
|
//
|
|
function quickEmail($insert_query) {
|
|
global $webdir, $current_domain_name;
|
|
|
|
$BASEDIR = $webdir;
|
|
$CurrentDomain = $current_domain_name;
|
|
|
|
// ---------- WILL BE A SERVER-WIDE SCRIPT ----------
|
|
include ("$BASEDIR/include/sendmail.php");
|
|
// include "sendmail.php";
|
|
// --------------------------------------------------
|
|
|
|
// insert the query into the email_header as "completed"
|
|
$r = db_query($insert_query);
|
|
$rid = mysql_insert_id();
|
|
|
|
// get the record to be processed
|
|
$query = "SELECT * FROM email_header WHERE record_num='$rid'";
|
|
$results = db_query($query);
|
|
|
|
// Get the list of fields in the email_header table
|
|
$cresults = mysql_query("show columns from email_header");
|
|
while ($crow = mysql_fetch_array($cresults)) {
|
|
if ($crow['Field'] == 'record_num') continue;
|
|
$hfields[] = $crow['Field'];
|
|
}
|
|
|
|
if ($results !== FALSE) {
|
|
$num_rows = mysql_num_rows($results);
|
|
|
|
if ($num_rows == 1) {
|
|
$hrow = mysql_fetch_array($results);
|
|
|
|
// Process the records and send the emails
|
|
$log_id = $hrow['record_num'];
|
|
$matching = $hrow['matching_records'];
|
|
$processed = $hrow['processed_records'];
|
|
$equery = $hrow['query'];
|
|
$table_name = $hrow['primary_table'];
|
|
$EmailSubject = stripslashes($hrow['subject']);
|
|
$EmailMessage = $hrow['message_text'];
|
|
|
|
// Get the set of email records to process this run
|
|
$eresults = mysql_query($equery);
|
|
|
|
// If the get was successful process the set
|
|
if ($eresults) {
|
|
|
|
// Determine how many records there are to process
|
|
$number = mysql_num_rows($eresults);
|
|
|
|
// If there are records to process the loop through them
|
|
if ($number) {
|
|
|
|
// Get other parameters from the header record
|
|
$ReplyTo = $hrow['reply_address'];
|
|
$ReplyDescription = $hrow['reply_description'];
|
|
$Message = $hrow['email_message_id'];
|
|
$EmailTemplate = $hrow['email_template'];
|
|
$EmailField = $hrow['email_field_name'];
|
|
|
|
$EmailAttach = array();
|
|
if ($hrow['attachments'] != '') {
|
|
$EmailAttach = explode(',', $hrow['attachments']);
|
|
}
|
|
|
|
// Get the list of fields in the primary table
|
|
$cresults = mysql_query("show columns from $table_name");
|
|
while ($crow = mysql_fetch_array($cresults)) {
|
|
$fields[] = $crow['Field'];
|
|
}
|
|
|
|
// Get the template file if required
|
|
if ($EmailTemplate != "TextMessage") {
|
|
if (!is_file("$BASEDIR/email_templates/$EmailTemplate")) {
|
|
echo("Error Opening Template: $BASEDIR/email_templates/$EmailTemplate\n");
|
|
exit();
|
|
}
|
|
|
|
$fp = fopen("$BASEDIR/email_templates/$EmailTemplate","r");
|
|
|
|
// If file opened successfully
|
|
if ($fp) {
|
|
|
|
while (!feof($fp)) {
|
|
$line = fgets($fp,1000);
|
|
|
|
$line = str_replace('src="/',"src=\"http://$CurrentDomain/",$line);
|
|
$line = str_replace("src='/","src='http://$CurrentDomain/",$line);
|
|
$EmailTemplateText .= $line;
|
|
}
|
|
|
|
// Merge the message to send with the template
|
|
$EmailMessage = str_replace("{EMAIL_BODY_TEXT}",$EmailMessage,$EmailTemplateText);
|
|
}
|
|
}
|
|
|
|
// Process generic variables into the email body
|
|
$EmailMessage = str_replace("{DATE1}",date("D M j, Y",time()),$EmailMessage);
|
|
$EmailMessage = str_replace("{DATE2}",date("M j, Y",time()),$EmailMessage);
|
|
$EmailMessage = str_replace("{DATE3}",date("l F j, Y",time()),$EmailMessage);
|
|
$EmailMessage = str_replace("{DATE4}",date("F j, Y",time()),$EmailMessage);
|
|
$EmailMessage = str_replace("{TIME1}",date("H:i",time()),$EmailMessage);
|
|
$EmailMessage = str_replace("{TIME2}",date("h:i a",time()),$EmailMessage);
|
|
|
|
// Process email_header values into the email template
|
|
for ($i = 0; $i < count($hfields); $i++) {
|
|
$fname = $hfields[$i];
|
|
$EmailMessage = preg_replace('#({|%7[Bb])' . $fname . '(}|%7[Dd])#',$hrow[$fname],$EmailMessage);
|
|
}
|
|
|
|
|
|
//
|
|
// Process each record from the result set
|
|
//
|
|
|
|
$count = 0;
|
|
$skipped = 0;
|
|
$sent = 0;
|
|
$errors = 0;
|
|
|
|
while ($row = mysql_fetch_array($eresults)) {
|
|
$email = trim($row[$EmailField]);
|
|
$count++;
|
|
|
|
// Insert the record now to get the record_num of the insert if there is one
|
|
$status = "Sent";
|
|
$query = "insert into email_send_log (email_log_id,email,processed,status) values ('$log_id','$email',now(),'$status')";
|
|
mysql_query($query);
|
|
$send_id = mysql_insert_id();
|
|
|
|
// Replace variables from current email record
|
|
$Message = $EmailMessage;
|
|
for ($i = 0; $i < count($fields); $i++) {
|
|
$fname = $fields[$i];
|
|
$Message = preg_replace('#({|%7[Bb])' . $fname . '(}|%7[Dd])#',$row[$fname],$Message);
|
|
}
|
|
|
|
// Replace system variables
|
|
$Message = preg_replace('#({|%7[Bb])TABLE_NAME(}|%7[Dd])#i',$table_name,$Message);
|
|
$Message = preg_replace('#({|%7[Bb])RECORD_NUM(}|%7[Dd])#i',$row[record_num],$Message);
|
|
$Message = preg_replace('#({|%7[Bb])EMAIL(}|%7[Dd])#i',$email,$Message);
|
|
$Message = preg_replace('#({|%7[Bb])EMAIL_COLUMN(}|%7[Dd])#i',$EmailField,$Message);
|
|
|
|
// Replace if has record_num field in DB
|
|
if ($send_id) {
|
|
$Message = preg_replace('#({|%7[Bb])LOG_ID(}|%7[Dd])#i',strval($send_id),$Message);
|
|
}
|
|
|
|
$Message = str_replace(""",'"',$Message);
|
|
$Message = stripslashes($Message);
|
|
|
|
// Parse image references to force full URL paths
|
|
$Message = str_replace("src=\"/","src=\"http://$CurrentDomain/",$Message);
|
|
$Message = str_replace("src='/","src='http://$CurrentDomain/",$Message);
|
|
|
|
if (strpos($Message, 'http://') !== false) {
|
|
$Message = preg_replace("#href=\"([a-z0-9])#i","href=\"http://$CurrentDomain/\\1",$Message);
|
|
$Message = preg_replace("#href='([a-z0-9])#i","href='http://$CurrentDomain/\\1",$Message);
|
|
$Message = preg_replace("#href=\"/#i","href=\"http://$CurrentDomain/",$Message);
|
|
$Message = preg_replace("#href='/#i","href='http://$CurrentDomain/",$Message);
|
|
}
|
|
|
|
// Process any email attachments
|
|
$attachments = array();
|
|
foreach ($EmailAttach as $attach => $attach_name) {
|
|
$attachments[$attach_name] = file_get_contents("$BASEDIR/attachments/$attach_name");
|
|
}
|
|
|
|
// Sets bounce back email if not already set
|
|
if (!$bounceReply) $bounceReply = $ReplyTo;
|
|
|
|
// Now send the email
|
|
if (SendEmail($email, $ReplyTo, $EmailSubject, $Message, $ReplyDescription, '', $attachments, $bounceReply)) {
|
|
$status = "Sent";
|
|
$sent++;
|
|
}
|
|
else {
|
|
$status = "Error";
|
|
$errors++;
|
|
|
|
// Update the record status if there is an error
|
|
$query = "update email_send_log " .
|
|
"set status = 'Error' " .
|
|
"where email_log_id = '$log_id' " .
|
|
"and email = '$email'";
|
|
mysql_query($query);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//
|
|
// Update the header record with the stats from this run
|
|
//
|
|
$query = "update email_header " .
|
|
"set processed_records = processed_records + $count, " .
|
|
"sent_successfully = sent_successfully + $sent, " .
|
|
"skipped = skipped + $skipped, " .
|
|
"errors = errors + $errors " .
|
|
"where record_num = '$log_id'";
|
|
mysql_query($query);
|
|
|
|
|
|
//
|
|
// Determine if the batch is complete
|
|
//
|
|
$query = "select * from email_header " .
|
|
"where record_num = '$log_id'";
|
|
$results = mysql_query($query);
|
|
$header = mysql_fetch_array($results);
|
|
|
|
$matching = $header[matching_records];
|
|
$processed = $header[processed_records];
|
|
}
|
|
|
|
if ($number == 0 || $matching == $processed) {
|
|
//
|
|
// If there are no more records to process then close the batch
|
|
//
|
|
$query = "update email_header " .
|
|
"set completed = now(), " .
|
|
"status = 'completed' " .
|
|
"where record_num = '$log_id'";
|
|
mysql_query($query);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|