Access Individual Fields - Contact Form - Swiftmailer So I followed the steps here https://drupal.stackexchange.com/a/199464/49661 to configure Drupal 8 to send contact form emails via Swiftmailer.

I created a template in my theme's template directory, swiftmailer.html.twig, which the emails do seem to be using. The issue is, I only seem to have access to all the fields at once using {{ body }}. This just spits all the field values out in one, giant mess. Random spacing everywhere, random new lines, some fields with labels, some without.

Ideally, I'd like to be able to access individual field values so I can template it. For example:

Full Name:
{{ fields.full_name.content }}


I've tried kinting a few variables out, but all body contains is the cluster of every field value together.

I've also tried creating more variables using [yourtheme]_swiftmailer_preprocess(&amp;$variables) after reading very little documenation available here https://www.drupal.org/node/1590184. This doesn't even seem to be ran when a contact form submission is made.

Here's what it looks like when I kint out body|raw:


Thanks for any help in advance
This definitely isn't a very nice solution, but it does "work".

I created a module, and added this into the .module file.

function modulename_mail_alter(&amp;$message)
{
  if(empty($message['params']['contact_message'])) {
    return false;
  }

  //$message['body'][1] = str_replace("\n\n\n\n",'',$message['body'][1]);
  //$message['body'][1] = str_replace('   ','',$message['body'][1]);
  $contactMessage = $message['params']['contact_message'];

  // Build message body
  //$message['body'][0] = [];
  $message['body'][1] = [];
  $message['body'][1] = "&lt;br&gt;&lt;br&gt;";

  if($contactMessage-&gt;hasfield('field_your_name')) {
    $message['body'][1] .= "&lt;br&gt;&lt;br&gt;Full Name:&lt;br&gt;";
    $message['body'][1] .= $contactMessage-&gt;get('field_your_name')-&gt;getString() . "&lt;br&gt;&lt;br&gt;";    
  }

  if($contactMessage-&gt;hasField('field_email_address')) {
    $message['body'][1] .= "Email Address:&lt;br&gt;";
    $message['body'][1] .= $contactMessage-&gt;get('field_email_address')-&gt;getString() . "&lt;br&gt;&lt;br&gt;";
  }

  if($contactMessage-&gt;hasField('field_telephone')) {
    $message['body'][1] .= "Telephone:&lt;br&gt;";
    $message['body'][1] .= $contactMessage-&gt;get('field_telephone')-&gt;getString() . "&lt;br&gt;&lt;br&gt;";
  }

  if($contactMessage-&gt;hasField('field_mobile')) {
    $message['body'][1] .= "Mobile:&lt;br&gt;";
    $message['body'][1] .= $contactMessage-&gt;get('field_mobile')-&gt;getString() . "&lt;br&gt;&lt;br&gt;";
  }

  if($contactMessage-&gt;hasField('field_company')) {
    $message['body'][1] .= "Company:&lt;br&gt;";
    $message['body'][1] .= $contactMessage-&gt;get('field_company')-&gt;getString() . "&lt;br&gt;&lt;br&gt;";
  }

  if($contactMessage-&gt;hasField('field_enquiry_type')) {
    $message['body'][1] .= "Enquiry Type:&lt;br&gt;";
    $message['body'][1] .= $contactMessage-&gt;get('field_enquiry_type')-&gt;getString() . "&lt;br&gt;&lt;br&gt;";
  }

  if($contactMessage-&gt;hasField('field_version')) {
    $message['body'][1] .= "Version:&lt;br&gt;";
    $message['body'][1] .= $contactMessage-&gt;get('field_version')-&gt;getString() . "&lt;br&gt;&lt;br&gt;";
  }  

  if($contactMessage-&gt;hasField('message')) {
    $message['body'][1] .= "Message:&lt;br&gt;";
    $message['body'][1] .= $contactMessage-&gt;get('message')-&gt;getString() . "&lt;br&gt;&lt;br&gt;";
  }  
}


So basically I clear the message, and then build it up again...