TestEmailCommand.php 3.2 KB

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