TestEmailCommand.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // TODO: remove when requiring Grav 1.7+
  59. if (method_exists($this, 'initializeGrav')) {
  60. $this->initializeThemes();
  61. }
  62. $grav = Grav::instance();
  63. $this->output->writeln('');
  64. $this->output->writeln('<yellow>Current Configuration:</yellow>');
  65. $this->output->writeln('');
  66. dump($grav['config']->get('plugins.email'));
  67. $this->output->writeln('');
  68. $grav['Email'] = new Email();
  69. $to = $this->input->getOption('to') ?: $grav['config']->get('plugins.email.to');
  70. $subject = $this->input->getOption('subject');
  71. $body = $this->input->getOption('body');
  72. if (!$subject) {
  73. $subject = 'Testing Grav Email Plugin';
  74. }
  75. if (!$body) {
  76. $configuration = print_r($grav['config']->get('plugins.email'), true);
  77. $body = $grav['language']->translate(['PLUGIN_EMAIL.TEST_EMAIL_BODY', $configuration]);
  78. }
  79. $sent = EmailUtils::sendEmail(['subject'=>$subject, 'body'=>$body, 'to'=>$to]);
  80. if ($sent) {
  81. $this->output->writeln("<green>Message sent successfully!</green>");
  82. } else {
  83. $this->output->writeln("<red>Problem sending email...</red>");
  84. }
  85. }
  86. }