Keeping track of number of recipients in a plugin?

I havenā€™t been able to find documentation on where the number of recipients is given to the plugin scope. This is likely a hidden feature, but I havenā€™t figured it out yet. I know thereā€™s a counters variable in processqueue.php that gets a part of itself sent to a total variable in the same file, but I havenā€™t figured out how this is accessible within a plugin or anywhere else in the code for that matter.

EDIT: I realize thereā€™s a processSendStats that sends the counters variable once every message has been sent, but itā€™s not clear how weā€™re supposed to use the data before messages process. In my scenario, you may want to know the total messages youā€™re planning to send before it happens so you can do custom logic for processing those messages.

1 Like

Interesting, it may be that a new plug-in hook will be necessary in phpList 3 to report this information at the time that you need it. Have you tried adding such a hook?

I donā€™t think I did it in an optimal way, but hereā€™s some bits and pieces of what I scribbled up in test files during my project.

In defaultplugin.php, after the processError function, I added:

/**
 * sendWhatWeGot
 * if mail sending plugin needs to, it will go ahead and send the data it is ready to send
 * this function is good if we need to suspend a message because it allows the plugin to finish sending what's processed already
 */
public function sendWhatWeGot()
{
	return false;
}

The idea here is that any plugin that is holding onto email data to be send in one go should override this functionality. It gets called in actions/processqueue.php. I wasnā€™t 100% sure where this functionality should be called, but I placed it around lines 1000 to 1020 within this code block:
$status = Sql_Fetch_Array_query(ā€œselect id,status from {$tables[ā€˜messageā€™]} where id = $messageidā€);
if (!$status[ā€˜idā€™]) {
ProcessError($GLOBALS[ā€˜I18Nā€™]->get(ā€˜Message I was working on has disappearedā€™));
} elseif ($status[ā€˜statusā€™] != ā€˜inprocessā€™) {
$script_stage = 6;
foreach($GLOBALS[ā€˜pluginsā€™] as $pluginname => $plugin) {
if ($plugin->sendWhatWeGot()) {
break;
}
}
ProcessError($GLOBALS[ā€˜I18Nā€™]->get(ā€˜Sending of this message has been suspendedā€™));
}
flush();

That allows us to be able to hold onto emails until the campaign ends or the campaign is paused. In order to tell the plugins how many messages there should be in total, I added this code in processqueue.php:

// now we have all our users to send the message to
$counters['total_users_for_message '.$messageid] = Sql_Affected_Rows();

if ($skipped >= 10000) {
    $counters['total_users_for_message '.$messageid] -= $skipped;
}
if (isset($counters['total_users_for_message '.$messageid])) {
    $total = $counters['total_users_for_message '.$messageid];
} else {
    $total = 0;
}
foreach($GLOBALS['plugins'] as $pluginname => $plugin) {
	$plugin->setTotal($total);
}

I apologize for the poor formatting. This forum does not seem to have a code tag for formatting code. I also apologize for any misinformation about how PHPList works. I had very little knowledge of the inner workings of this framework before starting this project, and thereā€™s probably a much better way of handling this data. This is just what worked for me.