Skip to content
Snippets Groups Projects
Commit 735f73c9 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3527050 by adamps: [Regression in 1.6] hook_mail_alter() cannot alter...

Issue #3527050 by adamps: [Regression in 1.6] hook_mail_alter() cannot alter email "to" header - breaks "Reroute Email" module
parent 1238b1e6
Branches
Tags
1 merge request!164Issue #3527050: hook_mail_alter() cannot alter email "to" header
Pipeline #509464 passed
......@@ -118,6 +118,12 @@ class LegacyMailerHelper implements LegacyMailerHelperInterface {
$src_headers = $message['headers'];
$dest_headers = $email->getHeaders();
// Add in 'To' header which is stored directly in the message.
// @see \Drupal\Core\Mail\Plugin\Mail\PhpMail::mail()
if (isset($message['to'])) {
$src_headers['to'] = $message['to'];
}
foreach ($src_headers as $name => $value) {
$name = strtolower($name);
if (isset(self::SKIP_HEADERS[$name])) {
......@@ -125,7 +131,7 @@ class LegacyMailerHelper implements LegacyMailerHelperInterface {
}
if (isset(self::ADDRESS_HEADERS[$name])) {
$email->setAddress($name, $this->importHelper->parseAddress($value));
$email->setAddress($name, $this->importHelper->parseAddress($value), TRUE);
}
else {
$dest_headers->addHeader($name, $value);
......@@ -136,6 +142,11 @@ class LegacyMailerHelper implements LegacyMailerHelperInterface {
if (isset($message['plain'])) {
$email->setTextBody($message['plain']);
}
// Parameters.
foreach ($message['params'] as $key => $value) {
$email->setParam($key, $value);
}
}
}
<?php
/**
* @file
* Mailer Override legacy email test install.
*/
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Implements hook_install().
*/
function mailer_override_legacy_test_install() {
ConfigurableLanguage::createFromLangcode('fr')->save();
}
......@@ -26,6 +26,7 @@ function mailer_override_legacy_test_mail($key, &$message, $params) {
function mailer_override_legacy_test_mail_alter(&$message) {
if ($message['id'] == 'mailer_override_legacy_test_legacy_test') {
$message['body'][] = 'This email was altered via hook_mail_alter().';
$message['to'] = LegacyTestEmailForm::ADDRESS_TO;
}
}
......
......@@ -70,7 +70,7 @@ class LegacyTestEmailForm extends FormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->mailManager->mail('mailer_override_legacy_test', 'legacy_test', self::ADDRESS_TO, 'en');
$this->mailManager->mail('mailer_override_legacy_test', 'legacy_test', 'wrong_value@example.com', 'fr');
}
}
......@@ -17,6 +17,7 @@ class LegacyEmailTest extends SymfonyMailerTestBase {
*/
protected static $modules = [
'mailer_override_legacy_test',
'language',
];
/**
......@@ -46,6 +47,10 @@ class LegacyEmailTest extends SymfonyMailerTestBase {
$this->assertCc(LegacyTestEmailForm::ADDRESS_CC);
$this->assertBcc(LegacyTestEmailForm::ADDRESS_BCC);
// Check language.
// @todo Should be 'fr' - will be fixed in [#3413081].
$this->assertLangcode('en');
// Check email contents.
$this->assertSubject("Legacy email sent via hook_mail().");
$this->assertBodyContains("This email is sent via hook_mail().");
......@@ -79,11 +84,6 @@ class LegacyEmailTest extends SymfonyMailerTestBase {
$this->submitForm([], 'Send test email');
$this->readMail();
// Check email recipients.
$this->assertTo(LegacyTestEmailForm::ADDRESS_TO);
$this->assertCc(LegacyTestEmailForm::ADDRESS_CC);
$this->assertBcc(LegacyTestEmailForm::ADDRESS_BCC);
// Check email contents.
$this->assertSubject("Legacy email sent via hook_mail().");
$this->assertBodyContains("This email is sent via hook_mail().");
......
......@@ -101,11 +101,16 @@ class BaseEmail implements BaseEmailInterface {
/**
* {@inheritdoc}
*
* The $legacy parameter is @internal and may be removed at any time.
*/
public function setAddress(string $name, $addresses): static {
public function setAddress(string $name, $addresses, bool $legacy = FALSE): static {
$name = strtolower($name);
assert(isset($this->addresses[$name]));
if ($name == 'to') {
// Allow late setting of the to address for legacy emails. The langcode
// will not be updated, however that is a limitation of the legacy mail
// system.
if (!$legacy && $name == 'to') {
$this->valid(EmailInterface::PHASE_INIT);
}
else {
......
......@@ -70,7 +70,7 @@ trait MailerTestTrait {
*
* @return $this
*/
public function assertSubject($value) {
public function assertSubject(string $value) {
$this->assertEquals($value, $this->email->getSubject());
return $this;
}
......@@ -180,6 +180,19 @@ trait MailerTestTrait {
return $this;
}
/**
* Checks langcode of the most recently sent email.
*
* @param string $value
* Text to check for.
*
* @return $this
*/
public function assertLangcode(string $value) {
$this->assertEquals($value, $this->email->getLangcode());
return $this;
}
/**
* Checks 'reply-to' address of the most recently sent email.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment