In some form of my custom module, a condition triggers an email to be sent:
public function save(array $form, FormStateInterface $form_state) { $entity = $this->entity; $status = parent::save($form, $form_state); $values=$form_state->getValues(); if (($values['isurgent']['value']==1) && ($values['status']['value']==1)) { $iSeliste = Drupal::currentUser()->id(); $oSeliste = Drupal::entityTypeManager()->getStorage('person')->load($iSeliste); $sSelite = $oSeliste->firstname->value . " " . $oSeliste->lastname->value; $sAction = ($values['action'][0]['value']==0)?"offre":"demande"; $sDueDate = $values['duedate'][0]['value']->format("d/m/Y"); _sendEmailForUrgentService($sSelite, $sAction, $sDueDate); }
in sel.module:
function _sendEmailForUrgentService($sSeliste, $sAction, $sDueDate) { ...some code... Drupal::service('plugin.manager.mail')->mail('sel', 'emailforurgentservice', $sTo, 'fr', $params); Drupal::logger('sel')->info('Courriel pour service urgent : envoi effectué.'); }
my hook_mail:
function sel_mail($key, &$message, $params) { $sFrom = '[redacted]'; $message['from'] = $sFrom; $message['headers'] = array( 'Content-Type' => 'text/html', 'bcc' => $params[3], 'From' => $sFrom, 'Sender' => $sFrom, 'Return-Path' => $sFrom, ); switch ($key) { case 'emailforurgentservice': $message['subject'] = '[Le Grenier à SÉL] Un service urgent requiert votre attention...'; $sBody = $params[0] . " a posté une " . $params[1] . " urgente qui est valide jusqu'au " . $params[2] . "."; $message['body'][] = check_markup(nl2br($sBody), 'full_html'); break; } }
The settings of mailsystem are these:
And in my theme templates folder (mysite/web/themes/contrib/mayo/templates/), there is my email template swiftmailer--sel--emailforurgentservice.html.twig
:
<html> <head> <style type="text/css"> table tr td { font-family: Candara; font-size: 14px; } </style> </head> <body> <div> <table width="800px" cellpadding="0" cellspacing="0"> <tr> <td> <div style="padding: 0px 0px 0px 0px;"> <p> Chers SÉListes,<br><br> {{ body }} <br> Vous pouvez la consulter <a href="http://lejardindepoissy.org/?q=sel_catalogue">ici</a>.";<br> Si vous ne la trouvez pas, c'est peut-être qu'elle a déjà été satisfaite.";<br><br> --<br> Le Grenier à SÉL </p> </div> </td> </tr> </table> </div> </body> </html>
The email is sent. Everything is OK (From, To, Bcc, Subject) except the body!
Instead what I defined in the template, I have only the content of $sBody
as defined in function sel_mail()
.
Any idea what I am doing wrong?
Sponsored by SupremePR