View source
<?php
namespace Drupal\mail_debugger\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MailDebuggerForm extends FormBase {
protected $storage;
protected $mailManager;
public function __construct(KeyValueFactoryInterface $storageFactory, MailManagerInterface $mailManager) {
$this->storage = $storageFactory
->get(static::class);
$this->mailManager = $mailManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('keyvalue'), $container
->get('plugin.manager.mail'));
}
public function getFormId() {
return "mail_debugger_form";
}
public function buildForm(array $form, FormStateInterface $form_state) {
return [
'to' => [
'#type' => 'email',
'#title' => $this
->t('To'),
'#required' => TRUE,
'#default_value' => $this->storage
->get("to"),
],
'subject' => [
'#type' => 'textfield',
'#title' => $this
->t('Subject'),
'#required' => TRUE,
'#default_value' => $this->storage
->get('subject'),
],
'body' => [
'#type' => 'textarea',
'#title' => $this
->t('Subject'),
'#required' => TRUE,
'#default_value' => $this->storage
->get('body'),
],
'actions' => [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this
->t("Send"),
],
],
];
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->storage
->set('to', $form_state
->getValue('to'));
$this->storage
->set('subject', $form_state
->getValue('subject'));
$this->storage
->set('body', $form_state
->getValue('body'));
$summary = $this->mailManager
->mail('mail_debugger', 'mail_debugger', $form_state
->getValue('to'), NULL, [
'subject' => $form_state
->getValue('subject'),
'body' => $form_state
->getValue('body'),
]);
if ($summary['result']) {
$this
->messenger()
->addStatus($this
->t("Sent a message."));
}
}
}