Files
HotLunches/email-batch-post.php
T
2026-10-19 15:22:52 -07:00

116 lines
4.0 KiB
PHP

<?php
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
header("Location: /");
exit();
}
// Ensure that only admin users have access to this module
$AllowedUserTypes = array('SuperUser','System Administrator','Application Administrator','Volunteer Administrator','Communications Administrator','Financial','Financial Team');
if (checkPermission($AllowedUserTypes) != 1) {
echo("<br>\n");
$error_message = getLanguageText('ERRORMSG-ACCESS-DENIED');
include "form.messages.php";
return;
}
// if ($_SESSION['UserInfo']['type'] == 'SuperUser') startLog("email-batch-post.php");
writeLog(print_r($_POST,true));
//
// Get the set of records to send emails to
//
$CurrentDomain = strtolower($_SERVER['HTTP_HOST']);
$Query = stripslashes($Query);
$results = db_query($Query);
if ($results) {
$num_rows = db_num_rows($results);
// Get the message to send
$content = db_query("SELECT * FROM formatted_content WHERE record_num = '$MessageID'");
$row = db_fetch_array($content);
$EmailSubject = db_escape_string($row["content_id"]);
$EmailMessage = db_escape_string($row["content"]);
// Process logged in user's details into email message
$ufields = array("first_name","last_name","email","title");
for ($i = 0; $i < count($ufields); $i++) {
$fname = $ufields[$i];
if (array_key_exists($fname, $_SESSION['UserInfo']))
$EmailMessage = preg_replace('/({|%7b)users.' . $fname . '(}|%7d)/i',$_SESSION['UserInfo'][$fname],$EmailMessage);
}
// Process custom fields into email message
foreach ($_POST as $fieldname => $fieldvalue) {
if (stripos('custom_', $fieldname) !== 0) continue;
$field_code = str_replace("custom_","",$fieldname);
$fieldvalue = db_escape_string(nl2br($fieldvalue));
$EmailMessage = preg_replace('/({|%7b)custom\.[a-z]+\.' . $field_code . '(}|%7d)/i',$fieldvalue,$EmailMessage);
}
$Category = db_escape_string($Category);
$Description = db_escape_string($Description);
$ReplyDescription = db_escape_string($ReplyDescription);
$attachments = db_escape_string($attachments);
writeLog("attachments: $attachments");
$groupid = 0;
if (array_key_exists('group_num',$_SESSION['UserInfo'])) $groupid = $_SESSION['UserInfo']['group_num'];
// $sendMode = "Email";
// if ($SendAfter == 'log-only') {
// }
// Write email_log_config record
$SendTime = strtotime($SendAfter);
$Query = db_escape_string($Query);
$current_user = $_SESSION['UserInfo']['record_num'];
$equery = "INSERT INTO email_header " .
"(school_num,category, description, reply_address, reply_description, email_message_id, subject, message_text, email_template, " .
"posted, send_after, send_after_time, matching_records, processed_records, sent_successfully, skipped, errors, email_field_name, primary_table, " .
"query, parameters, status, user_num, attachments) " .
"VALUES ($school_num,'$Category', '$Description', '$ReplyTo', '$ReplyDescription', '$MessageID', '$EmailSubject', '$EmailMessage', '$EmailTemplate', " .
"now(), '$SendAfter', $SendTime, '$num_rows', 0, 0, 0, 0, '$EmailField', '$PrimaryTable', '$Query', '$Parameters', 'open', $current_user, '$attachments')";
writeLog($equery);
db_query($equery);
$email_log_id = db_insert_id();
?>
<center>
<table width='90%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td align='center'>
<h3>Send Emails</h3>
<p><strong><?php echo($num_rows) ?> records found</strong></p>
<p>Email processing will start after: <?php echo($SendAfter) ?></p>
</td>
</tr>
<tr>
<td align='center'><a href='/?action=email-logs' class='btn btn--primary btn--xsmall'><span>View Logs</span></a></td>
</td>
</tr>
</table>
</center>
<br>
<br>
<br>
<?php
}
else {
?>
<p><strong>No matching records found</strong></p>
<?php
}
?>