Problem with Mail System/SwiftMailer
Question
In the submitForm of some form of my custom module, an email is sent this way:
$sTo = 'eric.falzon@free.fr';
$sBcc = _setListOfRecipients();
$aParams = array($sBcc, $rpYear);
\Drupal::service('plugin.manager.mail')->mail('association', 'renewmembershipfirst', $sTo, 'fr', $aParams);
In my custom module, there is
function association_mail($key, &$message, $aParams)
{
$sFrom = \Drupal::config('system.site')->get('mail');
$message['from'] = $sFrom;
$message['headers'] = array(
'Content-Type' => 'text/html',
'bcc' => $aParams[0],
'From' => $sFrom,
'Sender' => $sFrom,
'Return-Path' => $sFrom,
);
switch ($key) {
case 'renewmembershipfirst':
$message['subject'] = '[le Jardin de Poissy] Renouvellement d\'adhésion';
$sBody = "<b>" . $aParams[1] . "</b>";
$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--association--renewmembershipfirst.html.twig:
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<style type="text/css"> body {
font-family: 'Montserrat', sans-serif;
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 adhérents du <strong>Jardin de Poissy</strong>,<br>
<br>
La période de renouvellement des adhésions pour {{ body }} est ouverte !<br>
<br>
Pour nous signaler votre décision :
<ul>
<li>vous suivez ce <a href="{{ url('association.renew_membership') }}">{{ 'lien'|t }}</a>,</li>
<li>vous indiquez votre choix (Non, Oui),</li>
<li>vous soumettez.</li>
</ul>
En cas de réponse positive, la soumission déclenche le téléchargement de votre bulletin
d'adhésion.<br>
Vous l'imprimez et vous nous l'envoyez accompagné de votre règlement.<br>
<br>
Merci d'avance,<br>
<strong>Le Jardin de Poissy</strong><br><br>
P.-S. Ce courriel a été envoyé automatiquement. Merci de ne pas y répondre.
</p>
</div>
</td>
</tr>
</table>
</div>
</body>
</html>
The email is sent (and received). Everything is OK (From, To, Bcc, Subject, Body).
But (there is a but), I get this notice message
Notice: Undefined index: filter_format in Drupal\swiftmailer\Plugin\Mail\SwiftMailer->massageMessageBody() (line 630 of modules/contrib/swiftmailer/src/Plugin/Mail/SwiftMailer.php).
Drupal\swiftmailer\Plugin\Mail\SwiftMailer->massageMessageBody(Array) (Line: 107)
Drupal\swiftmailer\Plugin\Mail\SwiftMailer->format(Array) (Line: 43)
Drupal\mailsystem\Adapter->format(Array) (Line: 287)
Drupal\Core\Mail\MailManager->doMail('association', 'renewmembershipfirst', 'eric.falzon@free.fr', 'fr', Array, NULL, 1) (Line: 179)
Drupal\Core\Mail\MailManager->Drupal\Core\Mail\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 180)
Drupal\Core\Mail\MailManager->mail('association', 'renewmembershipfirst', 'eric.falzon@free.fr', 'fr', Array, NULL, 1) (Line: 73)
Drupal\mailsystem\MailsystemManager->mail('association', 'renewmembershipfirst', 'eric.falzon@free.fr', 'fr', Array) (Line: 301)
Drupal\association\Form\RenewMembership->executeActions(Array, Object)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 589)
Drupal\Core\Form\FormBuilder->processForm('renew_membership', Array, Object) (Line: 318)
Drupal\Core\Form\FormBuilder->buildForm('renew_membership', Object) (Line: 93)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Any idea what I am doing wrong?
Solution
I digged a litte bit in /swiftmailer/src/Plugin/Mail/SwiftMailer.php:
public function massageMessageBody(array $message) {
// Get default mail line endings and merge all lines in the e-mail body
// separated by the mail line endings. Keep Markup objects and escape others
// and then treat the result as safe markup.
$line_endings = Settings::get('mail_line_endings', PHP_EOL);
$applicable_format = $this->getApplicableFormat($message);
$filter_format = $this->config['message']['filter_format'];
$message['body'] = Markup::create(implode($line_endings, array_map(function ($body) use ($applicable_format, $filter_format) {
I went to $this->config['message'] with Devel' config editor:
format: text/html
respect_format: true
convert_mode: true
character_set: UTF-8
which stands probably for
But no trace of 'filter_format'.
Comparing with another D8 site I have with similar configuration, I found that what was missing is this entry in swiftmailer config: filter_format: plain_text
.
I added it with Devel and now everything is OK: no more Notice: Undefined index: filter_format in Drupal\swiftmailer\Plugin\Mail\SwiftMailer
.
But i have no clue of where this setting can be done thru UI.
Any idea?