/**
  * @return int|null|void
  */
 protected function serve()
 {
     $grav = Grav::instance();
     $this->output->writeln('');
     $this->output->writeln('<yellow>Current Configuration:</yellow>');
     $this->output->writeln('');
     dump($grav['config']->get('plugins.email'));
     $this->output->writeln('');
     require_once __DIR__ . '/../vendor/autoload.php';
     $grav['Email'] = new Email();
     $email_to = $this->input->getOption('to') ?: $grav['config']->get('plugins.email.to');
     $subject = $this->input->getOption('subject');
     $body = $this->input->getOption('body');
     if (!$subject) {
         $subject = 'Testing Grav Email Plugin';
     }
     if (!$body) {
         $configuration = print_r($grav['config']->get('plugins.email'), true);
         $body = $grav['language']->translate(['PLUGIN_EMAIL.TEST_EMAIL_BODY', $configuration]);
     }
     $sent = EmailUtils::sendEmail($subject, $body, $email_to);
     if ($sent) {
         $this->output->writeln("<green>Message sent successfully!</green>");
     } else {
         $this->output->writeln("<red>Problem sending email...</red>");
     }
 }
Example #2
0
 /**
  * Handle the email password recovery procedure.
  *
  * @return bool True if the action was performed.
  */
 protected function taskForgot()
 {
     $param_sep = $this->grav['config']->get('system.param_sep', ':');
     $data = $this->post;
     $username = isset($data['username']) ? $data['username'] : '';
     $user = !empty($username) ? User::load($username) : null;
     /** @var Language $l */
     $language = $this->grav['language'];
     $messages = $this->grav['messages'];
     if (!isset($this->grav['Email'])) {
         $messages->add($language->translate('PLUGIN_LOGIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error');
         $this->setRedirect('/');
         return true;
     }
     if (!$user || !$user->exists()) {
         $messages->add($language->translate(['PLUGIN_LOGIN.FORGOT_USERNAME_DOES_NOT_EXIST', $username]), 'error');
         $this->setRedirect($this->grav['config']->get('plugins.login.route_forgot'));
         return true;
     }
     if (empty($user->email)) {
         $messages->add($language->translate(['PLUGIN_LOGIN.FORGOT_CANNOT_RESET_EMAIL_NO_EMAIL', $username]), 'error');
         $this->setRedirect($this->grav['config']->get('plugins.login.route_forgot'));
         return true;
     }
     $token = md5(uniqid(mt_rand(), true));
     $expire = time() + 604800;
     // next week
     $user->reset = $token . '::' . $expire;
     $user->save();
     $author = $this->grav['config']->get('site.author.name', '');
     $fullname = $user->fullname ?: $username;
     $reset_link = $this->grav['base_url_absolute'] . $this->grav['config']->get('plugins.login.route_reset') . '/task:login.reset/token' . $param_sep . $token . '/user' . $param_sep . $username . '/nonce' . $param_sep . Utils::getNonce('reset-form');
     $sitename = $this->grav['config']->get('site.title', 'Website');
     $from = $this->grav['config']->get('plugins.email.from');
     if (empty($from)) {
         $messages->add($language->translate('PLUGIN_LOGIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error');
         $this->setRedirect($this->grav['config']->get('plugins.login.route_forgot'));
         return true;
     }
     $to = $user->email;
     $subject = $language->translate(['PLUGIN_LOGIN.FORGOT_EMAIL_SUBJECT', $sitename]);
     $content = $language->translate(['PLUGIN_LOGIN.FORGOT_EMAIL_BODY', $fullname, $reset_link, $author, $sitename]);
     $sent = EmailUtils::sendEmail($subject, $content, $to);
     if ($sent < 1) {
         $messages->add($language->translate('PLUGIN_LOGIN.FORGOT_FAILED_TO_EMAIL'), 'error');
     } else {
         $messages->add($language->translate(['PLUGIN_LOGIN.FORGOT_INSTRUCTIONS_SENT_VIA_EMAIL', $to]), 'info');
     }
     $this->setRedirect('/');
     return true;
 }
Example #3
0
 /**
  * Handle the email to activate the user account.
  *
  * @param User $user
  *
  * @return bool True if the action was performed.
  */
 public function sendActivationEmail($user)
 {
     if (empty($user->email)) {
         throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.USER_NEEDS_EMAIL_FIELD'));
     }
     $token = md5(uniqid(mt_rand(), true));
     $expire = time() + 604800;
     // next week
     $user->activation_token = $token . '::' . $expire;
     $user->save();
     $param_sep = $this->grav['config']->get('system.param_sep', ':');
     $activation_link = $this->grav['base_url_absolute'] . $this->config->get('plugins.login.route_activate') . '/token' . $param_sep . $token . '/username' . $param_sep . $user->username . '/nonce' . $param_sep . Utils::getNonce('user-activation');
     $site_name = $this->grav['config']->get('site.title', 'Website');
     $subject = $this->grav['language']->translate(['PLUGIN_LOGIN.ACTIVATION_EMAIL_SUBJECT', $site_name]);
     $content = $this->grav['language']->translate(['PLUGIN_LOGIN.ACTIVATION_EMAIL_BODY', $user->username, $activation_link, $site_name]);
     $to = $user->email;
     $sent = EmailUtils::sendEmail($subject, $content, $to);
     if ($sent < 1) {
         throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.EMAIL_SENDING_FAILURE'));
     }
     return true;
 }