Allow Sending Emails in Plain Text Format

Created on 18 March 2023, over 1 year ago
Updated 6 May 2023, over 1 year ago

I cannot find an option to send plain text e-mails. Can you add it to the module's settings?

Also, I propose adding parameters to $headers for From and Reply-to, to allow to set it from code.

For example, I need to send emails from users with the Manager role, and I need to set their emails as From/Reply-to, but I can't do that now.

Feature request
Status

Postponed: needs info

Version

2.0

Component

Code

Created by

🇷🇺Russia Andrew Answer Novosibirsk

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

Comments & Activities

  • Issue created by @Andrew Answer
  • 🇨🇦Canada Nathan Tsai

    Thank you for the feature request, Andrew. Good idea for plain text emails.

    We could do something like:

    
          $formats = \filter_formats($this->currentUser);
          $formatIDs = !empty($formats) ? array_keys($formats) : [];
          $form['emails'][$definitionName]['body'] = [
            '#type' => 'text_format',
            '#title' => $this->t('Body'),
            '#format' => 'full_html',
            '#description' => $this->t(
                '<strong>TWIG Variables: </strong> @variables. Use as <em>{{ variableName }}</em>',
                ['@variables' => implode(', ', $twigVariables)]
            ),
            '#default_value' => $emailConfig['body'] ?? $defaultBody,
            '#allowed_formats' => $formatIDs,
          ];
    

    To enable the `plain_text` format in the format list, store the format in the config, and if the format is plain text, send the email as plain text.

  • 🇨🇦Canada Nathan Tsai

    Also, I found that From and Reply-to headers do not work, because of you use lowercase in $headers array

    I did not know that the headers had to be capitalized. While the headers have been working fine for me on several production websites, let me quickly create an issue to fix this aspect.

    See: 🐛 Capitalize the Email Headers Fixed

    And, finally, I propose to add parameters to $headers for From and Reply-to, to allow to set it from code.

    The reason I have not allowed users to set "from" is because of spam issues. Specifically, if I am sending emails from the website "example.com" and the "from" address is "user@mysite.com", most email providers (e.g. Gmail) will flag the email as spam because the domains do not match (e.g. example.com != mysite.com).

    Thus, I only allow users to set the "replyTo" email address.

    At one point, I was considering some code like:

    private function getHeaders($replyTo) {
      $hostName = \Drupal::request()->getHost();
      if (self::endsWith($replyTo, '@' . $hostName)) {
        $from = $replyTo;
      }
      else {
        $from = $this->siteConfig->get('mail');
      }
      // Can not override manually.
      $headers = [
        'Content-Type' => 'text/html',
        'MIME-Version' => '1.0',
        'Reply-To' => $replyTo,
        'From' => $this->siteConfig->get('name') . ' <' . $from . '>',
      ];
    
      return $headers;
    }
    

    But I could not get it to work. Do you think it is worth exploring again?

  • 🇨🇦Canada Nathan Tsai

    Remove parts of issue related to 🐛 Capitalize the Email Headers Fixed

  • Status changed to Postponed: needs info over 1 year ago
Production build 0.71.5 2024