-
-
Save m4tlch/3ba0e493fd747cedebe26957fbe6aef7 to your computer and use it in GitHub Desktop.
Drupal 8 - Send mail with Swiftmailer
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 | |
| use Drupal\Core\Render\Markup; | |
| /** | |
| * Mail hook | |
| */ | |
| function hook_mail($key, &$message, $params) { | |
| $options['langcode'] = $message['langcode']; | |
| $headers = array( | |
| 'MIME-Version' => '1.0', | |
| 'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes', | |
| 'Content-Transfer-Encoding' => '8Bit', | |
| 'X-Mailer' => 'Drupal', | |
| ); | |
| switch($key) { | |
| case 'inline': | |
| $message['subject'] = t(''); | |
| // Add headers | |
| foreach ($headers as $key => $value) { | |
| $message['headers'][$key] = $value; | |
| } | |
| // Usage of inline markup | |
| $message['body'][] = Markup::create( '<b>'. t("Title") . '</b><br /><br />'); | |
| $message['body'][] = Markup::create( t('Text.').' <br />'); | |
| break; | |
| case 'template': | |
| $message['subject'] = t(''); | |
| // Add headers | |
| foreach ($headers as $key => $value) { | |
| $message['headers'][$key] = $value; | |
| } | |
| // Usage of template | |
| $renderer = \Drupal::service('renderer'); | |
| $render = [ | |
| '#theme' => 'module_template_mail', | |
| '#variables' => array(), | |
| ]; | |
| $message['body'][] = $renderer->renderRoot($render); | |
| break; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment