When submitting a message in my site contact form in Drupal 8.x I have printed a message

print_r($message['body'][1]);die;  //in my modulename_mail_alter(&$message)

I get the following message

Drupal\Core\Render\Markup Object
    [string:protected] => Message

   Test Message

     First Name

     Last Name


How can I change the body content of the email.

1 Answer 1


Alter how exactly? You can cast the markup object to a string, then you can change the string or completely replace it.

Also check out the default integration that the contact_storage module has with swiftmailer to allow sending mails as HTML. That might get you much further than trying to change this string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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