- 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 7:53pm 6 May 2023