Contact form auto-reply disabled when contact email available

Created on 4 May 2023, over 1 year ago
Updated 5 May 2023, over 1 year ago

Problem/Motivation

Auto-reply message won't be sent in contact form

Steps to reproduce

1. Go to /admin/structure/contact/
2. If no form exists create a new form with machine name contact and basic fields
3. Edit it going to /admin/structure/contact/manage/contact
4. Fill "Auto-reply" textarea with "test"
5. Log in your website, go to the contact form you created
6. Fill it in and send it.
7. You will receive an email with body "test" to the email you are logged in with
8. Now go to /admin/structure/contact/manage/contact/emails and add a new contact email (whichever)
9. Then repeat step 6: no email is received from auto-reply

Proposed resolution

I think that the reason is located in web/modules/contrib/contact_emails/contact_emails.module. In contact_emails_mail_alter it says "disable sending of default emails of we have at least one email set by contact emails". I guess the quickest solution would be to remove that condition? Or is it a feature? I would like to have emails sent to an administrator without losing the auto-reply functionality.

/**
 * Implements hook_mail_alter().
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function contact_emails_mail_alter(&$message) {
  // If an email from the contact module, disable sending.
  $contact_form_ids = [
    'contact_page_autoreply',
    'contact_page_mail',
  ];
  if (in_array($message['id'], $contact_form_ids)) {
    /** @var \Drupal\contact\MessageInterface $contact_message */
    if ($contact_message = $message['params']['contact_message']) {
      $contact_form = $contact_message->getContactForm();
      $contact_form_id = $contact_form->id();
      /** @var \Drupal\contact_emails\ContactEmailStorageInterface $storage */
      $storage = \Drupal::entityTypeManager()->getStorage('contact_email');
      if ($storage->hasContactEmails($contact_form_id)) {
        // Disable sending of default mails if we have at least one email
        // set by contact_emails (regardless of whether or not it is a disabled
        // email).
        $message['send'] = FALSE;
      }
    }
    else {
      // If we don't have a contact_message object, something must have gone
      // wrong, let's disable just in case.
      $log = t('Unable to load the contact message when attempting to alter the default mailing from contact form for contact form id: @contact_form_id', ['@contact_form_id' => $message['id']]);
      \Drupal::logger('contact_emails')->notice($log);
      $message['send'] = FALSE;
    }
  }
🐛 Bug report
Status

Active

Version

1.21

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024