<commit_before><?php

/**
 * Class used to release email from the queue
 *
 * @package   ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
 *
 * @version 2.0 dev
 *
 */

namespace ElkArte\Mail;

/**
 * Sends emails from the mail queue in to the wild
 */
class QueueMail
{
	/**
	 * Sends a group of emails from the mail queue.
	 *
	 * - Allows a batch of emails to be released every 5 to 10 seconds (based on per period limits)
	 * - If batch size is not set, will determine a size such that it sends in 1/2 the period (buffer)
	 * - Handles using a cron job and another script to send the emails ("mail_queue_use_cron" setting)
	 *
	 * @param int|bool $batch_size = false the number to send each loop
	 * @param bool $override_limit = false bypassing our limit flaf
	 * @param bool $force_send = false
	 * @return bool
	 * @package Mail
	 */
	public function reduceMailQueue($batch_size = false, $override_limit = false, $force_send = false)
	{
		global $modSettings;

		// Do we have another script to send out the queue?
		if (!empty($modSettings['mail_queue_use_cron']) && empty($force_send))
		{
			return false;
		}

		// If we came with a timestamp, and that doesn't match the next event, then someone else has beaten us.
		if (isset($_GET['ts']) && $_GET['ts'] != $modSettings['mail_next_send'] && empty($force_send))
		{
			return false;
		}

		// How many emails can we send each time we are called in a period
		$batch_size = $this->setBatchSize($batch_size);

		// Set the delay / pause until the next sending
		$delay = $this->setDelay($override_limit);
		if ($delay === false)
		{
			return false;
		}

		// Make any needed adjustments based on quota remaining in this time period.
		$batch_size = $this->adjustBatchSize($override_limit, $batch_size, $delay);

		// Now we know how many we're sending, let's send them.
		require_once(SUBSDIR . '/Mail.subs.php');
		list ($ids, $emails) = emailsInfo($batch_size);

		// Remove these from the queue .... Delete, delete, delete!!!
		if (!empty($ids))
		{
			deleteMailQueueItems($ids);
		}

		// Don't believe we have any left after this batch?
		if (count($ids) < $batch_size)
		{
			resetNextSendTime();
		}

		// Nothing to send?
		if (empty($ids))
		{
			return false;
		}

		// Prepare to send each email, and log that for future-proof.
		require_once(SUBSDIR . '/Maillist.subs.php');

		// We have some to send, lets send them!
		$failed_emails = array();
		$mail = new Mail();
		foreach ($emails as $email)
		{
			// Enable PBE processing if this is a maillist mailing
			if (!empty($modSettings['maillist_enabled'])<commit_msg>! helps to put that in the right spot fix4fix<commit_after><?php

/**
 * Class used to release email from the queue
 *
 * @package   ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
 *
 * @version 2.0 dev
 *
 */

namespace ElkArte\Mail;

/**
 * Sends emails from the mail queue in to the wild
 */
class QueueMail
{
	/**
	 * Sends a group of emails from the mail queue.
	 *
	 * - Allows a batch of emails to be released every 5 to 10 seconds (based on per period limits)
	 * - If batch size is not set, will determine a size such that it sends in 1/2 the period (buffer)
	 * - Handles using a cron job and another script to send the emails ("mail_queue_use_cron" setting)
	 *
	 * @param int|bool $batch_size = false the number to send each loop
	 * @param bool $override_limit = false bypassing our limit flaf
	 * @param bool $force_send = false
	 * @return bool
	 * @package Mail
	 */
	public function reduceMailQueue($batch_size = false, $override_limit = false, $force_send = false)
	{
		global $modSettings;

		// Do we have another script to send out the queue?
		if (!empty($modSettings['mail_queue_use_cron']) && empty($force_send))
		{
			return false;
		}

		// If we came with a timestamp, and that doesn't match the next event, then someone else has beaten us.
		if (isset($_GET['ts']) && $_GET['ts'] != $modSettings['mail_next_send'] && empty($force_send))
		{
			return false;
		}

		require_once(SUBSDIR . '/Mail.subs.php');

		// How many emails can we send each time we are called in a period
		$batch_size = $this->setBatchSize($batch_size);

		// Set the delay / pause until the next sending
		$delay = $this->setDelay($override_limit);
		if ($delay === false)
		{
			return false;
		}

		// Make any needed adjustments based on quota remaining in this time period.
		$batch_size = $this->adjustBatchSize($override_limit, $batch_size, $delay);

		// Now we know how many we're sending, let's send them.
		list ($ids, $emails) = emailsInfo($batch_size);

		// Remove these from the queue .... Delete, delete, delete!!!
		if (!empty($ids))
		{
			deleteMailQueueItems($ids);
		}

		// Don't believe we have any left after this batch?
		if (count($ids) < $batch_size)
		{
			resetNextSendTime();
		}

		// Nothing to send?
		if (empty($ids))
		{
			return false;
		}

		// Prepare to send each email, and log that for future-proof.
		require_once(SUBSDIR . '/Maillist.subs.php');

		// We have some to send, lets send them!
		$failed_emails = array();
		$mail = new Mail();
		foreach ($emails as $email)
		{
			// Enable PBE processing if this is a maillist mailing
			if (!empty($modSettings['maillist_enabled'])
{'fraction_non_alphanumeric': 0.11917480198931664, 'fraction_numerical': 0.0031313317369681343, 'mean_word_length': 3.3825665859564165, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5506396', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1677, 'n_words': 630}