TestEmailCommand.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Grav\Plugin\Console;
  3. use Grav\Common\Grav;
  4. use Grav\Console\ConsoleCommand;
  5. use Grav\Plugin\Email\Email;
  6. use Grav\Plugin\Email\Utils as EmailUtils;
  7. use Symfony\Component\Console\Input\InputOption;
  8. /**
  9. * Class TestEmailCommand
  10. * @package Grav\Console\Cli\
  11. */
  12. class TestEmailCommand extends ConsoleCommand
  13. {
  14. /**
  15. * @var array
  16. */
  17. protected $options = [];
  18. /**
  19. *
  20. */
  21. protected function configure()
  22. {
  23. $this
  24. ->setName('test-email')
  25. ->setAliases(['testemail'])
  26. ->addOption(
  27. 'to',
  28. 't',
  29. InputOption::VALUE_REQUIRED,
  30. 'An email address to send the email to'
  31. )
  32. ->addOption(
  33. 'env',
  34. 'e',
  35. InputOption::VALUE_OPTIONAL,
  36. 'The environment to trigger a specific configuration. For example: localhost, mysite.dev, www.mysite.com'
  37. )
  38. ->addOption(
  39. 'subject',
  40. 's',
  41. InputOption::VALUE_OPTIONAL,
  42. 'A subject for the email'
  43. )
  44. ->addOption(
  45. 'body',
  46. 'b',
  47. InputOption::VALUE_OPTIONAL,
  48. 'The body of the email'
  49. )
  50. ->setDescription('Sends a test email using the plugin\'s configuration')
  51. ->setHelp('The <info>test-email</info> command sends a test email using the plugin\'s configuration');
  52. }
  53. /**
  54. * @return int|null|void
  55. */
  56. protected function serve()
  57. {
  58. $grav = Grav::instance();
  59. $this->output->writeln('');
  60. $this->output->writeln('<yellow>Current Configuration:</yellow>');
  61. $this->output->writeln('');
  62. dump($grav['config']->get('plugins.email'));
  63. $this->output->writeln('');
  64. require_once __DIR__ . '/../vendor/autoload.php';
  65. $grav['Email'] = new Email();
  66. $email_to = $this->input->getOption('to') ?: $grav['config']->get('plugins.email.to');
  67. $subject = $this->input->getOption('subject');
  68. $body = $this->input->getOption('body');
  69. if (!$subject) {
  70. $subject = 'Testing Grav Email Plugin';
  71. }
  72. if (!$body) {
  73. $configuration = print_r($grav['config']->get('plugins.email'), true);
  74. $body = $grav['language']->translate(['PLUGIN_EMAIL.TEST_EMAIL_BODY', $configuration]);
  75. }
  76. $sent = EmailUtils::sendEmail($subject, $body, $email_to);
  77. if ($sent) {
  78. $this->output->writeln("<green>Message sent successfully!</green>");
  79. } else {
  80. $this->output->writeln("<red>Problem sending email...</red>");
  81. }
  82. }
  83. }