Skip to content

Instantly share code, notes, and snippets.

@pglatz
Last active May 13, 2026 23:18
Show Gist options
  • Select an option

  • Save pglatz/a4bedd8e51b446019f5b84bb9e8e292f to your computer and use it in GitHub Desktop.

Select an option

Save pglatz/a4bedd8e51b446019f5b84bb9e8e292f to your computer and use it in GitHub Desktop.
code sample - test_email

email_test

Create a test email.

CONTENTS OF THIS FILE

  • Introduction
  • Requirements
  • Installation
  • Configuration
  • Maintainers

INTRODUCTION

Site is custom module for testing email sending. It presents a form where user can enter a test mail.

REQUIREMENTS

This module requires no modules outside of Drupal core.

INSTALLATION

CONFIGURATION

No configuration is required.

MAINTAINERS

Current maintainers:

$ ls -R README.md src test_email.info.yml test_email.links.menu.yml test_email.module test_email.routing.yml

./src: Form

./src/Form: TestEmailForm.php 16:05 ~/dev/Servers/pglatz/johnbutton-www/web/modules/custom/test_email $

name: Test Email
type: module
description: 'Simple form to test sending emails.'
core_version_requirement: ^10 || ^11
package: Electronovelty
<?php
/**
* @file
* Test email module.
*/
declare(strict_types=1);
/**
* Implements hook_mail().
*/
function test_email_mail(string $key, array &$message, array $params): void {
if ($key === 'test') {
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
}
}
test_email.form:
path: '/admin/config/development/test-email'
defaults:
_form: 'Drupal\test_email\Form\TestEmailForm'
_title: 'Test Email'
requirements:
_permission: 'administer site configuration'
<?php
declare(strict_types=1);
namespace Drupal\test_email\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form for testing email sending.
*/
class TestEmailForm extends FormBase {
public function __construct(
protected MailManagerInterface $mailManager,
protected StateInterface $state,
) {}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): static {
return new static(
$container->get('plugin.manager.mail'),
$container->get('state'),
);
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'test_email_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$form['recipient'] = [
'#type' => 'email',
'#title' => $this->t('Recipient'),
'#required' => TRUE,
'#default_value' => $this->state->get('test_email.recipient', ''),
];
$form['subject'] = [
'#type' => 'textfield',
'#title' => $this->t('Subject'),
'#required' => TRUE,
'#default_value' => $this->state->get('test_email.subject', 'Test Email'),
];
$form['body'] = [
'#type' => 'textarea',
'#title' => $this->t('Body'),
'#required' => TRUE,
'#default_value' => 'This is a test email sent from Drupal.',
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Send Test Email'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$recipient = $form_state->getValue('recipient');
$subject = $form_state->getValue('subject');
$body = $form_state->getValue('body');
// Store values for next time.
$this->state->set('test_email.recipient', $recipient);
$this->state->set('test_email.subject', $subject);
$params = [
'subject' => $subject,
'body' => $body,
];
$result = $this->mailManager->mail(
'test_email',
'test',
$recipient,
$this->currentUser()->getPreferredLangcode(),
$params,
);
if ($result['result']) {
$this->messenger()->addStatus($this->t('Test email sent to @recipient.', ['@recipient' => $recipient]));
}
else {
$this->messenger()->addError($this->t('Failed to send test email.'));
}
}
}

Comments are disabled for this gist.