Preserve Custom Email Headers

Created on 2 February 2022, over 2 years ago
Updated 15 March 2023, over 1 year ago

Problem/Motivation

I have a custom module that sends an email, through SendGrid, to a mailing list. That list requires certain headers to be present in the email message. For example X-Something: Something. These are not being set by the module when the message is built for SendGrid.

Steps to reproduce

In my custom module's .module file I have

function thursday_mail($key, &$message, $params) {

  $message['from'] = 'test@example.com';
  $message['body'] = $params['body'];
  $message['headers'] = [
    'X-Testing' => 'just testing!',
    'X-Mailer' => 'ThrusdayMail'
  ];

  switch ($key) {
    default:
      $message['subject'] = 'DEFAULT: ' . $params['subject'];
  }

}

When the email is delivered to my inbox, those headers are no longer in the message source.

Proposed resolution

In src/Plugin/Mail/SendGridMail.php, in the mail() function, there is

// Beginning of consolidated header parsing.
    foreach ($message['headers'] as $key => $value) {
      switch (mb_strtolower($key)) {

That switch only looks for "content-type" and "reply-to" and ignores anything else. Any other headers are not added to $sendgrid_message and disappear.

I propose adding the following to the bottom of the switch statement.

default:
  $sendgrid_message->addHeader($key, $value);

In my testing, this does pass on the headers, and they are present in the message source when the email gets to my inbox.

If there's another way to accomplish the same thing, please let me know. Thank you!

✨ Feature request
Status

Closed: outdated

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States ethangeorgi

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024