0

I'm using the core contact forms module in Drupal 8, and what I need to do is add a select with multiple options, each option needing to change the recipient address.

How can I do this using the built-in module, or do I need a different module?

4

The contact_storage module has a feature that allows you to create a list that include a key, value and e-mail and then the mail is sent to that.

You can either use that, or you can do it yourself in a similar way. You could use a term reference for example and have a field on your terms for the e-mail. The relevant code is in contact_storage_mail_alter():

$contact_message = $message['params']['contact_message'];
foreach ($contact_message->getFields() as $field) {
  if ($field->getFieldDefinition()->getType() === 'contact_storage_options_email') {
    // One of the Option email item fields. Add the recipient to the message.
    foreach ($field as $delta => $item) {
      $label = $item->value;
      // Obtain the email to add to the message, using the label.
      $email = $item->getFieldDefinition()->getSetting('allowed_values')[$label]['emails'];
      $message['to'] .= ',' . $email;
    }
  }
}

}

  • I worked it out using the field Contact Storage gives you. I've answered my question with the way I used. I'll upvote your answer though. – Daniel Dewhurst Aug 30 '16 at 14:38
3

After a lot of playing around and digging about, I've actually found out that it's quite easy to do this. After installing the Contact Storage module, when editing a contact forms' fields, you can add a field named Options email.

You then are given a large text area to input keys, labels and email addresses.

Support Query|Support Query|example@example.com
Sales Enquiry|Sales Enquiry|example@example.com
Admin/Accounts Query|Admin/Accounts Query|example@example.com
Partner Query|Partner Query|example@example.com

After adding the above, I was given a select to pick from one of my options. This successfully changed the email recipient.

  • I'm using this and it seems to work. One issue is that it won't accept a comma separated list of email addresses – James Oct 21 '16 at 11:57
  • I've tried this too, but after one message was sent you can't change the list anymore... :/ And also every e-mail is still sent to the main address of the contact form. – 0711master Feb 23 '17 at 12:37
  • @0711master, I thought this was true as well, but despite the message saying "There is data for this field in the database. The field settings can no longer be changed.", you can in fact still add more "key|label|email" entries on the Field Settings page successfully by adding them and clicking "Save field settings". It works! – Wesley Musgrove Apr 14 '17 at 15:25

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.