1

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(&$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: I've pixelated the sensitive information

Thanks for any help in advance

  • Did you manage to solve this? I have had a similar problem with emails generated by forms being full of newlines. – James Oct 11 '16 at 11:08
  • @James I was working in a timeframe and wasn't able to find a nice solution. I did end up being able to access single fields though. I'll write an answer. – Daniel Dewhurst Oct 12 '16 at 15:25
1

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(&$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] = "<br><br>";

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

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

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

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

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

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

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

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

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

|improve this answer|||||
0

Finally found a solution for this problem:

{{ message.params.contact_message.get('field_name').getValue().0.value }}

With this you can access any field on your form!

Hope it helps :)

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.