Created
June 2, 2025 11:17
-
-
Save opi/a5a6d2cee2c2166709421d6f9a85aaeb to your computer and use it in GitHub Desktop.
Drupal Symfony Mailer - Custom Header Email Adjuster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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