<commit_before><?php

namespace eland\task;

use League\HTMLToMarkdown\HtmlConverter;
use eland\queue;
use Monolog\Logger;
use eland\this_group;
use eland\mailaddr;
use Twig_Environment as Twig;

class mail
{
	protected $converter;
	protected $mailer;
	protected $queue;
	protected $monolog;
	protected $this_group;
	protected $mailaddr;
	protected $twig;

	public function __construct(queue $queue, Logger $monolog, this_group $this_group, mailaddr $mailaddr, Twig $twig)
	{
		$this->queue = $queue;
		$this->monolog = $monolog;
		$this->this_group = $this_group;
		$this->mailaddr = $mailaddr;
		$this->twig = $twig;

		$enc = getenv('SMTP_ENC') ?: 'tls';
		$transport = \Swift_SmtpTransport::newInstance(getenv('SMTP_HOST'), getenv('SMTP_PORT'), $enc)
			->setUsername(getenv('SMTP_USERNAME'))
			->setPassword(getenv('SMTP_PASSWORD'));

		$this->mailer = \Swift_Mailer::newInstance($transport);

		$this->mailer->registerPlugin(new \Swift_Plugins_AntiFloodPlugin(100, 30));

		$this->converter = new HtmlConverter();
		$converter_config = $this->converter->getConfig();
		$converter_config->setOption('strip_tags', true);
		$converter_config->setOption('remove_nodes', 'img');
	}

	/**
	 *
	 */
	public function process(array $data)
	{
		if (!isset($data['schema']))
		{
			$app->monolog->error('mail error: mail in queue without schema');
			return;
		}

		$sch = $data['schema'];
		unset($data['schema']);

		if (!readconfigfromdb('mailenabled', $sch))
		{
			$m = 'Mail functions are not enabled. ' . "\n";
			echo $m;
			$this->monolog->error('mail: ' . $m, ['schema' => $sch]);
			return ;
		}

		if (isset($data['template']) && isset($data['vars']))
		{
			$template_subject = $this->twig->loadTemplate('mail/' . $data['template'] . '.subject.twig');
			$template_html = $this->twig->loadTemplate('mail/' . $data['template'] . '.html.twig');
			$template_text = $this->twig->loadTemplate('mail/' . $data['template'] . '.text.twig');

			$data['subject']  = $template_subject->render($data['vars']);
			$data['html'] = $template_html->render($data['vars']);
			$data['text'] = $template_text->render($data['vars']);
		}
		else if (isset($data['template_from_config']) && isset($data['vars']))
		{
			$template = readconfigfromdb($data['template_from_config'], $sch);

			if (!$template)
			{
				$this->monolog->error('mail error: no template set in config for ' . $data['template_from_config'],
					['schema' => $sch]);
				return;
			}

			try
			{
				$template_subject = $this->twig->loadTemplate('mail/' . $data['template_from_config'] . '.subject.twig');
				$template_html = $this->twig->createTemplate($template);

				$data['subject']  = $template_subject->render($data['vars']);
				$data['html'] = $template_html->render($data['vars']);
				$data['text'] = $this->converter->convert($data['html']);
			}
			catch (Exception $e)
			{
				$this->monolog->error('Fout in mail template: ' . $e->getMessage(), ['schema' => $schema]);
				return;
			}
		}
		else
		{
			if (!isset($data['subject']))
			{
				$this->monolog->error('mail error: mail without subject', ['schema' => $sch]);
				return;
			}

			if (!isset($data['text']))
			{
				if (isset($data['html']))
				{
					$data['text'] = $this->converter->convert($data['html']);
				}
				else
				{
					$this->monolog->error('mail error: mail without body content', ['schema' => $sch]);
					return;
				}
			}
		}

		if (!$data['to'])
		{
			$this->monolog->error('mail error: mail without "to" | subject: ' . $data['subject'], ['schema' => $sch]);
			return;
		}
<commit_msg>fix schema in error message<commit_after><?php

namespace eland\task;

use League\HTMLToMarkdown\HtmlConverter;
use eland\queue;
use Monolog\Logger;
use eland\this_group;
use eland\mailaddr;
use Twig_Environment as Twig;

class mail
{
	protected $converter;
	protected $mailer;
	protected $queue;
	protected $monolog;
	protected $this_group;
	protected $mailaddr;
	protected $twig;

	public function __construct(queue $queue, Logger $monolog, this_group $this_group, mailaddr $mailaddr, Twig $twig)
	{
		$this->queue = $queue;
		$this->monolog = $monolog;
		$this->this_group = $this_group;
		$this->mailaddr = $mailaddr;
		$this->twig = $twig;

		$enc = getenv('SMTP_ENC') ?: 'tls';
		$transport = \Swift_SmtpTransport::newInstance(getenv('SMTP_HOST'), getenv('SMTP_PORT'), $enc)
			->setUsername(getenv('SMTP_USERNAME'))
			->setPassword(getenv('SMTP_PASSWORD'));

		$this->mailer = \Swift_Mailer::newInstance($transport);

		$this->mailer->registerPlugin(new \Swift_Plugins_AntiFloodPlugin(100, 30));

		$this->converter = new HtmlConverter();
		$converter_config = $this->converter->getConfig();
		$converter_config->setOption('strip_tags', true);
		$converter_config->setOption('remove_nodes', 'img');
	}

	/**
	 *
	 */
	public function process(array $data)
	{
		if (!isset($data['schema']))
		{
			$app->monolog->error('mail error: mail in queue without schema');
			return;
		}

		$sch = $data['schema'];
		unset($data['schema']);

		if (!readconfigfromdb('mailenabled', $sch))
		{
			$m = 'Mail functions are not enabled. ' . "\n";
			echo $m;
			$this->monolog->error('mail: ' . $m, ['schema' => $sch]);
			return ;
		}

		if (isset($data['template']) && isset($data['vars']))
		{
			$template_subject = $this->twig->loadTemplate('mail/' . $data['template'] . '.subject.twig');
			$template_html = $this->twig->loadTemplate('mail/' . $data['template'] . '.html.twig');
			$template_text = $this->twig->loadTemplate('mail/' . $data['template'] . '.text.twig');

			$data['subject']  = $template_subject->render($data['vars']);
			$data['html'] = $template_html->render($data['vars']);
			$data['text'] = $template_text->render($data['vars']);
		}
		else if (isset($data['template_from_config']) && isset($data['vars']))
		{
			$template = readconfigfromdb($data['template_from_config'], $sch);

			if (!$template)
			{
				$this->monolog->error('mail error: no template set in config for ' . $data['template_from_config'],
					['schema' => $sch]);
				return;
			}

			try
			{
				$template_subject = $this->twig->loadTemplate('mail/' . $data['template_from_config'] . '.subject.twig');
				$template_html = $this->twig->createTemplate($template);

				$data['subject']  = $template_subject->render($data['vars']);
				$data['html'] = $template_html->render($data['vars']);
				$data['text'] = $this->converter->convert($data['html']);
			}
			catch (Exception $e)
			{
				$this->monolog->error('Fout in mail template: ' . $e->getMessage(), ['schema' => $sch]);
				return;
			}
		}
		else
		{
			if (!isset($data['subject']))
			{
				$this->monolog->error('mail error: mail without subject', ['schema' => $sch]);
				return;
			}

			if (!isset($data['text']))
			{
				if (isset($data['html']))
				{
					$data['text'] = $this->converter->convert($data['html']);
				}
				else
				{
					$this->monolog->error('mail error: mail without body content', ['schema' => $sch]);
					return;
				}
			}
		}

		if (!$data['to'])
		{
			$this->monolog->error('mail error: mail without "to" | subject: ' . $data['subject'], ['schema' => $sch]);
			return;
		}

{'fraction_non_alphanumeric': 0.2089594158123859, 'fraction_numerical': 0.001404297149276787, 'mean_word_length': 4.225238444607483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 113, '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': '4295538', 'n_tokens_mistral': 2817, 'n_tokens_neox': 2448, 'n_words': 438}