<commit_before><?php

namespace queue;

use queue\queue_interface;
use League\HTMLToMarkdown\HtmlConverter;
use service\queue;
use Monolog\Logger;
use Twig_Environment as Twig;
use service\config;
use service\mail_addr_system;
use service\token;
use service\email_validate;

class mail implements queue_interface
{
	protected $converter;
	protected $mailer;
	protected $queue;
	protected $monolog;
	protected $twig;
	protected $config;
	protected $mail_addr_system;
	protected $email_validate;

	public function __construct(
		queue $queue,
		Logger $monolog,
		Twig $twig,
		config $config,
		mail_addr_system $mail_addr_system,
		email_validate $email_validate
	)
	{
		$this->queue = $queue;
		$this->monolog = $monolog;
		$this->twig = $twig;
		$this->config = $config;
		$this->mail_addr_system = $mail_addr_system;
		$this->email_validate = $email_validate;

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

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

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

		$this->mailer->getTransport()->stop();

		$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):void
	{
		if (!isset($data['schema']))
		{
			$app->monolog->error('mail queue proces: no schema. ' .
				json_encode($data));
			return;
		}

		$sch = $data['schema'];

		if (!$this->config->get('mailenabled', $sch))
		{
			$m = 'E-mail functions are not enabled. ' . "\n";
			echo $m;
			$this->monolog->error('mail queue proces: mail functions not enabled. ' .
				json_encode($data),
				['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['text'] = $template_text->render($data['vars']);
			$data['html'] = $template_html->render($data['vars']);
		}
		else if (isset($data['template_from_config']) && isset($data['vars']))
		{
			$template = $this->config->get($data['template_from_config'], $sch);

			if (!$template)
			{
				$this->monolog->error('mail queue process: no template set in config. ' .
					json_encode($data),
					['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['text'] = $this->converter->convert($data['html']);
				$data['html'] = $template_html->render($data['vars']);
			}
			catch (Exception $e)
			{
				$this->monolog->error('mail queue process, config template err: ' .
					$e->getMessage() . ' ::: ' .
					json_encode($data),
					['schema' => $sch]);
				return;
			}
		}
		else
		{
			if (!isset($data['subject']))
			{
				$this->monolog->error('mail queue process: mail without subject' .
					json_encode($data),
					['schema' => $sch]);
				return;
			}

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

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

		if (!$data['from'])
		{
			$this->monolog->error('mail queue process: mail without "from" ' .
				json_encode($data),
				['schema' => $sch]);
			return;
		}

		$message = (new \Swift_Message())
			->setSubject($data['subject'])
			->setBody($data['text'])
			->setTo($data['to'])
			->setFrom($data['from']);

		if (isset($data['html']))
		{
			$message->addPart($data['html'], 'text/html');
		}

		if (isset($data['reply_to']))
		{
			$message->setReplyTo($data['reply_to']);
		}

		if (isset($data['cc']))
		{
			$message->setCc($data['cc']);
		}

		try
		{
			if ($this->mailer->send($message, $failed_recipients))
			{
				$this->monolog->info('mail queue process: sent ' .
					implode(', ', $data['to']) . ' subject: ' . $data['subject'],
					['schema' => $sch]);
			}
			else
			{
				$this->monolog->error('mail queue process: failed sending message ' .
					json_encode($data),
					['schema' => $sch]);
				$this->monolog->error('mail queue process, failed recipients: ' .
					json_encode($failed_recipients),
					['schema' => $sch]);
			}
		}
		catch (Exception $e)
		{
			$err = $e->getMessage();
			error_log('mail queue process: ' . $err);
			$this->monolog->error('mail queue process: ' . $err . ' | ' .
				json_encode($data),
				['schema' => $sch]);<commit_msg>log email in send message<commit_after><?php

namespace queue;

use queue\queue_interface;
use League\HTMLToMarkdown\HtmlConverter;
use service\queue;
use Monolog\Logger;
use Twig_Environment as Twig;
use service\config;
use service\mail_addr_system;
use service\token;
use service\email_validate;

class mail implements queue_interface
{
	protected $converter;
	protected $mailer;
	protected $queue;
	protected $monolog;
	protected $twig;
	protected $config;
	protected $mail_addr_system;
	protected $email_validate;

	public function __construct(
		queue $queue,
		Logger $monolog,
		Twig $twig,
		config $config,
		mail_addr_system $mail_addr_system,
		email_validate $email_validate
	)
	{
		$this->queue = $queue;
		$this->monolog = $monolog;
		$this->twig = $twig;
		$this->config = $config;
		$this->mail_addr_system = $mail_addr_system;
		$this->email_validate = $email_validate;

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

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

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

		$this->mailer->getTransport()->stop();

		$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):void
	{
		if (!isset($data['schema']))
		{
			$app->monolog->error('mail queue proces: no schema. ' .
				json_encode($data));
			return;
		}

		$sch = $data['schema'];

		if (!$this->config->get('mailenabled', $sch))
		{
			$m = 'E-mail functions are not enabled. ' . "\n";
			echo $m;
			$this->monolog->error('mail queue proces: mail functions not enabled. ' .
				json_encode($data),
				['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['text'] = $template_text->render($data['vars']);
			$data['html'] = $template_html->render($data['vars']);
		}
		else if (isset($data['template_from_config']) && isset($data['vars']))
		{
			$template = $this->config->get($data['template_from_config'], $sch);

			if (!$template)
			{
				$this->monolog->error('mail queue process: no template set in config. ' .
					json_encode($data),
					['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['text'] = $this->converter->convert($data['html']);
				$data['html'] = $template_html->render($data['vars']);
			}
			catch (Exception $e)
			{
				$this->monolog->error('mail queue process, config template err: ' .
					$e->getMessage() . ' ::: ' .
					json_encode($data),
					['schema' => $sch]);
				return;
			}
		}
		else
		{
			if (!isset($data['subject']))
			{
				$this->monolog->error('mail queue process: mail without subject' .
					json_encode($data),
					['schema' => $sch]);
				return;
			}

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

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

		if (!$data['from'])
		{
			$this->monolog->error('mail queue process: mail without "from" ' .
				json_encode($data),
				['schema' => $sch]);
			return;
		}

		$message = (new \Swift_Message())
			->setSubject($data['subject'])
			->setBody($data['text'])
			->setTo($data['to'])
			->setFrom($data['from']);

		if (isset($data['html']))
		{
			$message->addPart($data['html'], 'text/html');
		}

		if (isset($data['reply_to']))
		{
			$message->setReplyTo($data['reply_to']);
		}

		if (isset($data['cc']))
		{
			$message->setCc($data['cc']);
		}

		try
		{
			if ($this->mailer->send($message, $failed_recipients))
			{
				$this->monolog->info('mail queue process: sent ' .
					json_encode($data['to']) . ' subject: ' . $data['subject'],
					['schema' => $sch]);
			}
			else
			{
				$this->monolog->error('mail queue process: failed sending message ' .
					json_encode($data),
					['schema' => $sch]);
				$this->monolog->error('mail queue process, failed recipients: ' .
					json_encode($failed_recipients),
					['schema' => $sch]);
			}
		}
		catch (Exception $e)
		{
			$err = $e->getMessage();
			error_log('mail queue process: ' . $err);
			$this->monolog->error('mail queue process: ' . $err . ' | ' .
				json_encode($data),
				['schema' => $sch]);
{'fraction_non_alphanumeric': 0.2067605633802817, 'fraction_numerical': 0.0009389671361502347, 'mean_word_length': 3.7316748111950244, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 179, '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': '712023', 'n_tokens_mistral': 4335, 'n_tokens_neox': 3638, 'n_words': 648}