Skip to content

Instantly share code, notes, and snippets.

@opi
Created June 2, 2025 11:17
Show Gist options
  • Save opi/a5a6d2cee2c2166709421d6f9a85aaeb to your computer and use it in GitHub Desktop.
Save opi/a5a6d2cee2c2166709421d6f9a85aaeb to your computer and use it in GitHub Desktop.
Drupal Symfony Mailer - Custom Header Email Adjuster
<?php
namespace Drupal\MY_MODULE\Plugin\EmailAdjuster;
use Drupal\Core\Form\FormStateInterface;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\Processor\EmailAdjusterBase;
/**
* Defines the Custom header Email Adjuster.
*
* @EmailAdjuster(
* id = "custom_header",
* label = @Translation("Custom Header"),
* description = @Translation("Sets the email custom header."),
* )
*/
class CustomHeaderEmailAdjuster extends EmailAdjusterBase {
/**
* {@inheritdoc}
*/
public function build(EmailInterface $email) {
$header_name = $this->configuration['name'];
$header_value = $this->configuration['value'];
// addHeader or addTextHeader ?
$email->getHeaders()->addTextHeader($header_name, $header_value);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['name'] = [
'#title' => $this->t('Custom header name'),
'#type' => 'textfield',
'#default_value' => $this->configuration['name'] ?? NULL,
'#required' => TRUE,
];
$form['value'] = [
'#title' => $this->t('Custom header value'),
'#type' => 'textfield',
'#default_value' => $this->configuration['value'] ?? NULL,
'#required' => TRUE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function getSummary() {
return $this->configuration['name'] . ': ' . $this->configuration['value'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment