TestEmailCommand.php 3.2 KB

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