|
| 1 | +<?php |
| 2 | +namespace Prokl\BitrixOrdinaryToolsBundle\Commands; |
| 3 | + |
| 4 | +use CSiteCheckerTest; |
| 5 | +use Symfony\Component\Console\Command\Command; |
| 6 | +use Symfony\Component\Console\Input\InputInterface; |
| 7 | +use Symfony\Component\Console\Output\OutputInterface; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class EmailsCanBeSendCommand |
| 11 | + * |
| 12 | + * @package Prokl\BitrixOrdinaryToolsBundle |
| 13 | + * |
| 14 | + * @since 27.08.2021 |
| 15 | + */ |
| 16 | +class EmailsCanBeSendCommand extends Command |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @inheritDoc |
| 20 | + */ |
| 21 | + public function configure() |
| 22 | + { |
| 23 | + $this->setName('bitrix:check-send-email') |
| 24 | + ->setDescription('Check mail sending'); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @inheritDoc |
| 29 | + */ |
| 30 | + public function execute(InputInterface $input, OutputInterface $output): int |
| 31 | + { |
| 32 | + $output->writeln('Testing capability of email sending started...'); |
| 33 | + |
| 34 | + $siteChecker = new CSiteCheckerTest; |
| 35 | + if (!$siteChecker->check_mail()) { |
| 36 | + $output->writeln('Sending email failed.'); |
| 37 | + } |
| 38 | + |
| 39 | + if (!$siteChecker->check_mail_big()) { |
| 40 | + $output->writeln('Sending big email failed.'); |
| 41 | + } |
| 42 | + |
| 43 | + $result = $this->runCheck($output); |
| 44 | + if ($result) { |
| 45 | + $output->writeln('Testing sending of email finished successfully.'); |
| 46 | + } else { |
| 47 | + $output->writeln('Testing sending of email failed.'); |
| 48 | + } |
| 49 | + |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param OutputInterface $output |
| 55 | + * |
| 56 | + * @return bool |
| 57 | + */ |
| 58 | + private function runCheck(OutputInterface $output) : bool |
| 59 | + { |
| 60 | + $defaultMailResult = false; |
| 61 | + $largeMailResult = false; |
| 62 | + if (function_exists('mail')) { |
| 63 | + $emailTo = 'hosting_test@bitrixsoft.com'; |
| 64 | + $subject = 'testing mail server'; |
| 65 | + $message = 'testing mail server'; |
| 66 | + $headers = 'From: webmaster@example.com' . "\r\n" . |
| 67 | + 'Reply-To: webmaster@example.com' . "\r\n" . |
| 68 | + 'X-Mailer: PHP/' . phpversion(); |
| 69 | + |
| 70 | + $defaultMailResult = mail($emailTo, $subject, $message, $headers); |
| 71 | + if (!$defaultMailResult) { |
| 72 | + $output->writeln('Mail server not configured.'); |
| 73 | + } |
| 74 | + |
| 75 | + $largeMailResult = (new CSiteCheckerTest)->check_mail_big(); |
| 76 | + if (!$largeMailResult) { |
| 77 | + $output->writeln('Sending big email failed'); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return $defaultMailResult && $largeMailResult; |
| 82 | + } |
| 83 | +} |
0 commit comments