[Drupal] How to send a notification mail to the corresponding email id when a users adds a feedback?

| | 3 min read

While working on a Drupal project my task was to enable the Drupal feedback module and when a feedback is added by a user, send a mail to corresponding email id. However in the feedback module there in no option to send mail by default. In this article I will be explaining how to send a notification mail to the corresponding email id when a user adds a feedback.

Lets start with the usual procedure download and enable Feedback Module. Now you can configure this module in "admin/config/user-interface/feedback" page. You can view the feedback reports in "admin/reports/feedback" page.

Now comes the mail sending part. Hope you have a custom module, if not please create one.

First step is to get the form id of feedback form and create a submit handler for this form using form_alter().

function modulename_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'feedback_form') {
    $form['#submit'][] = 'modulename_feedback_form_submit';
  }
}

In the submit handler will say what action is to be performed when form is clicked. Here in submit handler will call the function modulename_mail_send().

function modulename_feedback_form_submit($form, &$form_state) {
  modulename_mail_send($form_state['values']);
}

Now lets implement the hook_mail(). In this hook, $key is an argument passed which is an unique identifier which helps in indentifying each e-mail. $message is another argument passed which helps to set the subject and body for our email.

/**
 * Implement hook_mail().
 *
 */
function modulename_mail($key, &$message, $params) {
  global $user;
  $options = array(
    'langcode' => $message['language']->language,
  );

  switch ($key) {
    case 'modulename_feedback_form_submit':
      $message['subject'] = t('A new Feedback addded in site @site-name', array('@site-name' => variable_get('site_name', 'Drupal')), $options);
      $message['body'][] = t('@name added the following feedback:', array('@name' => $user->name), $options);
      $message['body'][] = check_plain($params['message']);
      break;
  }
}

Now lets implement the function mail_send. In this function we define the following arguments.

  • $module -> Need to specify the module (mirrored from hook_mail()
  • $key -> Need to specify the template key (mirrored from hook_mail())
  • $to -> Enter 'to' addresses
  • $from -> Enter 'from' addresses
  • $params -> It gets the values we entered in hook_mail().
  • $language -> Select the default language.
  • $send -> If value is TRUE will automatically send the mail when drupal_mail_send() is called
  • $result -> To checke whether mail has been send when drupal_mail_send() is called.
/**
 * Sends an e-mail.
 *
 */
function modulename_mail_send($form_values) {
  $module = 'modulename';
  $key = 'modulename_feedback_form_submit';
  $to = variable_get('modulename_feedback_form_email_id');
  $from = variable_get('site_mail', '[email protected]');
  $params = $form_values;
  $language = language_default();
  $send = TRUE;
  $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);

  if ($result['result'] == TRUE) {
    drupal_set_message(t('Your message has been sent.'));
  }
  else {
    drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
  }

}

So far everything is good. The problem here is "to" address has to entered manually in code, so each time if the admin has to change the address who receives the email when a feedback is added, has to change the code which is a bad practice.

In-order to overcome this problem we implemented a hook_menu to call a custom form to enter the email id and displayed it in admin menu. Form name is "modulename_feedback_form_settings". Here form is displayed in page "admin/feedback/settings" make sure there is no other forms in this page.

/**
 * Implementation of hook_menu().
 */
function modulename_menu() {
  $items['admin/feedback/settings'] = array(
      'title' => 'Enter Email ID',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('modulename_feedback_form_settings'),
      'description' => "Enter Email ID to get drupal feedback message.",
      'access callback' => 'user_access',
      'access arguments' => array('access administration menu'),
  );

  return $items;
}

In this form a textfield is used to enter the email id and valu is stored in variable "modulename_feedback_form_email_id". As a return of this function system_settings_form() is called. So that when a user click save button. Value from text field will automaticaly will get updated in system table. The value can be called as "variable_get('modulename_feedback_form_email_id')".

function modulename_feedback_form_settings() {
    $form['modulename_feedback_form_email_id'] = array(
      '#type'             => 'textfield',
      '#title'            => t('Enter Email ID.'),
      '#default_value'    => variable_get('modulename_feedback_form_email_id'),
      '#description'      => t('Enter a email id to receive feedback mail'),
    );
    return system_settings_form($form);
}

Why don't you try to do this in your site and see how its works. Feel free to send in your suggestions and doubts.

Reference URL:http://api.drupal.org/api/examples/email_example%21email_example.module/7.