doEmailTask(999999);
echo "Completed Mailout ID: ".$_GET['id'];
exit;
}
$action = $e107->tp->toDB(varset($_GET['mode'],'makemail'));
$pageMode = varset($_GET['savepage'], $action); // Sometimes we need to know what brought us here - $action gets changed
$mailId = intval(varset($_GET['m'],0));
$targetId = intval(varset($_GET['t'],0));
// Create mail admin object, load all mail handlers
$mailAdmin = new mailoutAdminClass($action); // This decodes parts of the query using $_GET syntax
e107::setRegistry('_mailout_admin', $mailAdmin);
if ($mailAdmin->loadMailHandlers() == 0)
{ // No mail handlers loaded
echo 'No mail handlers loaded!!';
exit;
}
require_once(e_ADMIN.'auth.php');
$errors = array();
$subAction = '';
$midAction = '';
$fromHold = FALSE;
if (isset($_POST['mailaction']))
{
if (is_array($_POST['mailaction']))
{
foreach ($_POST['mailaction'] as $k => $v)
{
if ($v) // Look for non-empty action
{
$mailId = $k;
$action = $v;
break;
}
}
}
}
if (isset($_POST['targetaction']))
{
if (is_array($_POST['targetaction']))
{
foreach ($_POST['targetaction'] as $k => $v)
{
if ($v) // Look for non-empty action
{
$targetId = $k;
$action = $v;
break;
}
}
}
}
//echo "Action: {$action} MailId: {$mailId} Target: {$targetId}
";
// ----------------- Actions ------------------->
//TODO - replace code sections with class/functions.
switch ($action)
{
case 'prefs' :
if (getperms('0'))
{
if (isset($_POST['testemail']))
{ // Send test email - uses standard 'single email' handler
if(trim($_POST['testaddress']) == '')
{
$emessage->add(LAN_MAILOUT_19, E_MESSAGE_ERROR);
$subAction = 'error';
}
else
{
$mailheader_e107id = USERID;
require_once(e_HANDLER.'mail.php');
$add = ($pref['mailer']) ? " (".strtoupper($pref['mailer']).")" : ' (PHP)';
$sendto = trim($_POST['testaddress']);
if (!sendemail($sendto, LAN_MAILOUT_113." ".SITENAME.$add, LAN_MAILOUT_114,LAN_MAILOUT_189))
{
$emessage->add(($pref['mailer'] == 'smtp') ? LAN_MAILOUT_67 : LAN_MAILOUT_106, E_MESSAGE_ERROR);
}
else
{
$emessage->add(LAN_MAILOUT_81. ' ('.$sendto.')', E_MESSAGE_SUCCESS);
$admin_log->log_event('MAIL_01',$sendto,E_LOG_INFORMATIVE,'');
}
}
}
elseif (isset($_POST['updateprefs']))
{
saveMailPrefs($emessage);
}
}
break;
case 'mailcopy' : // Copy existing email and go to edit screen
if (isset($_POST['mailaction']))
{
$action = 'makemail';
$mailData = $mailAdmin->retrieveEmail($mailId);
if ($mailData === FALSE)
{
$emessage->add(LAN_MAILOUT_164.':'.$mailId, E_MESSAGE_ERROR);
break;
}
unset($mailData['mail_source_id']);
}
break;
case 'mailedit' : // Edit existing mail
if (isset($_POST['mailaction']))
{
$action = 'makemail';
$mailData = $mailAdmin->retrieveEmail($mailId);
if ($mailData === FALSE)
{
$emessage->add(LAN_MAILOUT_164.':'.$mailId, E_MESSAGE_ERROR);
break;
}
}
break;
case 'makemail' :
$newMail = TRUE;
if (isset($_POST['save_email']))
{
$subAction = 'new';
}
elseif (isset($_POST['update_email']))
{
$subAction = 'update';
$newMail = FALSE;
}
elseif (isset($_POST['send_email']))
{ // Send bulk email
$subAction = 'send';
}
if ($subAction != '')
{
$mailData = $mailAdmin->parseEmailPost($newMail);
$errors = $mailAdmin->checkEmailPost($mailData, $subAction == 'send'); // Full check if sending email
if ($errors !== TRUE)
{
$subAction = 'error';
break;
}
$mailData['mail_selectors'] = $mailAdmin->getAllSelectors(); // Add in the selection criteria
}
// That's the checking over - now do something useful!
switch ($subAction)
{
case 'send' : // This actually creates the list of recipients in the display routine
$action = 'marksend';
break;
case 'new' :
// TODO: Check all fields created - maybe
$mailData['mail_content_status'] = MAIL_STATUS_SAVED;
$mailData['mail_create_app'] = 'core';
$result = $mailAdmin->saveEmail($mailData, TRUE);
if (is_numeric($result))
{
$mailData['mail_source_id'] = $result;
$emessage->add(LAN_MAILOUT_145, E_MESSAGE_SUCCESS);
}
else
{
$emessage->add(LAN_MAILOUT_146, E_MESSAGE_ERROR);
}
break;
case 'update' :
$mailData['mail_content_status'] = MAIL_STATUS_SAVED;
$result = $mailAdmin->saveEmail($mailData, FALSE);
if (is_numeric($result))
{
$mailData['mail_source_id'] = $result;
$emessage->add(LAN_MAILOUT_147, E_MESSAGE_SUCCESS);
}
else
{
$emessage->add(LAN_MAILOUT_146, E_MESSAGE_ERROR);
}
break;
}
break;
case 'mailhold' :
$action = 'held';
if ($mailAdmin->holdEmail($mailId))
{
$emessage->add(str_replace('--ID--', $mailId, LAN_MAILOUT_229), E_MESSAGE_SUCCESS);
}
else
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_230);
}
break;
case 'mailcancel' :
$action = $pageMode; // Want to return to some other page
if ($mailAdmin->cancelEmail($mailId))
{
$emessage->add(str_replace('--ID--', $mailId, LAN_MAILOUT_220), E_MESSAGE_SUCCESS);
}
else
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_221);
}
break;
case 'maildelete' :
break;
case 'marksend' : // Actually do something with an email and list of recipients - entry from email confirm page
$action = 'saved';
if (isset($_POST['email_cancel'])) // 'Cancel' in this context means 'delete' - don't want it any more
{
$midAction = 'midDeleteEmail';
}
elseif (isset($_POST['email_hold']))
{
if ($mailAdmin->activateEmail($mailId, TRUE))
{
$emessage->add(str_replace('--ID--', $mailId, LAN_MAILOUT_187), E_MESSAGE_SUCCESS);
}
else
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_166);
}
$action = 'held';
}
elseif (isset($_POST['email_send']))
{
$midAction = 'midMoveToSend';
$action = 'pending';
}
if(isset($_POST['email_sendnow']))
{
$midAction = 'midMoveToSend';
//$action = 'pending';
}
break;
case 'mailsendnow' : // Send mail previously on 'held' list. Need to give opportunity to change time/date etc
$action = 'marksend'; // This shows the email details for confirmation
$fromHold = TRUE;
$mailData['mail_source_id'] = $mailId;
break;
case 'maildeleteconfirm' :
$action = $pageMode; // Want to return to some other page
$midAction = 'midDeleteEmail';
if (!isset($_POST['mailIDConf']) || (intval($_POST['mailIDConf']) != $mailId))
{
$errors[] = str_replace(array('--ID--', '--CHECK--'), array($mailId, intval($_POST['mailIDConf'])), LAN_MAILOUT_174);
break;
}
break;
case 'mailonedelete' :
case 'debug' :
$emessage->add('Not implemented yet', E_MESSAGE_ERROR);
break;
case 'mailtargets' :
$action = 'recipients';
// Intentional fall-through
case 'recipients' :
case 'saved' : // Show template emails - probably no actions
case 'sent' :
case 'pending' :
case 'held' :
case 'mailshowtemplate' :
if (isset($_POST['etrigger_ecolumns']))
{
$mailAdmin->mailbodySaveColumnPref($action);
}
break;
case 'maint' : // Perform any maintenance actions required
if (isset($_POST['email_dross']))
if ($mailAdmin->dbTidy()) // Admin logging done in this routine
{
$emessage->add(LAN_MAILOUT_184, E_MESSAGE_SUCCESS);
}
else
{
$errors[] = LAN_MAILOUT_183;
}
break;
// Send Emails Immediately using Ajax
case 'mailsendimmediately' :
$id = array_keys($_POST['mailaction']);
sendImmediately($id[0]);
break;
default :
$emessage->add('Code malfunction 23! ('.$action.')', E_MESSAGE_ERROR);
$e107->ns->tablerender(LAN_MAILOUT_97, $emessage->render());
exit; // Could be a hack attempt
} // switch($action) - end of 'executive' tasks
// ------------------------ Intermediate actions ---------------------------
// (These have more than one route to trigger them)
switch ($midAction)
{
case 'midDeleteEmail' :
// $emessage->add($pageMode.': Would delete here: '.$mailId, E_MESSAGE_SUCCESS);
// break; // Delete this
$result = $mailAdmin->deleteEmail($mailId, 'all');
$admin_log->log_event('MAIL_04','ID: '.$mailId,E_LOG_INFORMATIVE,'');
if (($result === FALSE) || !is_array($result))
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_166);
}
else
{
if (isset($result['content']))
{
if ($result['content'] === FALSE)
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_167);
}
else
{
$emessage->add(str_replace('--ID--', $mailId, LAN_MAILOUT_167), E_MESSAGE_SUCCESS);
}
}
if (isset($result['recipients']))
{
if ($result['recipients'] === FALSE)
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_169);
}
else
{
$emessage->add(str_replace(array('--ID--', '--NUM--'), array($mailId, $result['recipients']), LAN_MAILOUT_170), E_MESSAGE_SUCCESS);
}
}
}
break;
case 'midMoveToSend' :
$notify = isset($_POST['mail_notify_complete']) ? 3 : 2;
$first = 0;
$last = 0; // Set defaults for earliest and latest send times.
// TODO: Save these fields
if (isset($_POST['mail_earliest_time']))
{
$first = e107::getDateConvert()->decodeDateTime($_POST['mail_earliest_time'], 'datetime', CORE_DATE_ORDER, FALSE);
}
if (isset($_POST['mail_latest_time']))
{
$last = e107::getDateConvert()->decodeDateTime($_POST['mail_earliest_time'], 'datetime', CORE_DATE_ORDER, TRUE);
}
if ($mailAdmin->activateEmail($mailId, FALSE, $notify, $first, $last))
{
$emessage->add(LAN_MAILOUT_185, E_MESSAGE_SUCCESS);
$admin_log->log_event('MAIL_06','ID: '.$mailId,E_LOG_INFORMATIVE,'');
}
else
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_188);
}
break;
}
if(isset($_POST['email_sendnow']))
{
sendImmediately($mailId);
}
// --------------------- Display errors and results ------------------------
if (is_array($errors) && (count($errors) > 0))
{
foreach ($errors as $e)
{
$emessage->add($e, E_MESSAGE_ERROR);
}
unset($errors);
}
if ($emessage->hasMessage())
{
$ns->tablerender(LAN_MAILOUT_97, $emessage->render());
}
// ---------------------- Display required page ----------------------------
// At this point $action determines which page display is required - one of a
// fairly limited number of choices
$mailAdmin->newMode($action);
//echo "Action: {$action} MailId: {$mailId} Target: {$targetId}
";
switch ($action)
{
case 'prefs' :
if (getperms('0'))
{
show_prefs($mailAdmin);
}
break;
case 'maint' :
if (getperms('0'))
{
show_maint(FALSE);
}
break;
case 'debug' :
if (getperms('0'))
{
show_maint(TRUE);
}
break;
case 'saved' : // Show template emails
case 'sent' :
case 'pending' :
case 'held' :
$mailAdmin->showEmailList($action, -1, -1);
break;
case 'mailshowtemplate' : // Show the templated email
$mailAdmin->showEmailTemplate($mailId);
break;
case 'maildelete' : // NOTE:: need to set previous page in form
$mailAdmin->showDeleteConfirm($mailId, $pageMode);
break;
case 'marksend' : // Show the send confirmation page
$mailAdmin->sendEmailCircular($mailData, $fromHold);
break;
case 'recipients' :
$mailAdmin->showmailRecipients($mailId, $action);
break;
case 'makemail' :
default :
if (!isset($mailData) || !is_array($mailData))
{
$mailData = array(); // Empty array just in case
}
$mailAdmin->show_mailform($mailData);
break;
}
require_once(e_ADMIN.'footer.php');
/**
* Real-time Immediate Mail-out. Browser may be closed and will continue.
* @param integer $id (mailing id)
* @return
*/
function sendImmediately($id)
{
global $emessage;
$text = "