Skip to content

Instantly share code, notes, and snippets.

@WengerK
Created June 6, 2016 14:09
Show Gist options
  • Save WengerK/29caee97961494b1a68f18d0392cb3e2 to your computer and use it in GitHub Desktop.
Save WengerK/29caee97961494b1a68f18d0392cb3e2 to your computer and use it in GitHub Desktop.
Drupal 8 - Send mail with Swiftmailer
<?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