drupal-7drupal-webformdrupal-rules

Drupal 7 - Send an E-mail to user on content modification


I have a webform page where user submits its query. On form submission a confirmation mail is sent to the user and admin. I have installed the Custom Webform Comment module through which user and admin both can comment and change the status in the form, now I want to send mail on each comment and status change to both admin and user.

I have tried using Webform Rules module but it is not working. The module is not sending mail

Is there any other way or module(s) through which I can send mail to the user?

Webform version 7.x-4.15

Thanks in advance :)


Solution

  • I resolved the above issue by inserting hook_mail code in my Custom Webform Comment module.

    In case anyone else is facing the same issue here is the code which I inserted in the custom_webform_comments.module file.

    /**
     * Implements hook_mail().
     */
    function custom_webform_comments_mail($key, &$message, $params) {
      switch($key) {
        case 'custom_webform_comments_email':
          $message['subject'] = t('Submit Request Form update');
          $message['body'][] = t('You have got a new comment and status update on your submitted form.',array('@site-name' => variable_get('site_name','example.com')));
           $message['body'][] = t(variable_get('custom_webform_comments_email_text',''));
          break;
      }
    }
    

    After the above line of code put the following code after the database insert query inside the function custom_webform_comments_commentform_submit() to send mail after every comment update.

    function custom_webform_comments_commentform_submit($form, $form_state) {
      $fv = $form_state['values'];
      $insert1 = db_insert('custom_webform_comments')
        ->fields(array(
          'cid' => NULL,
          'comment' => $fv['addnew']['comment'],
          'subject' => $fv['addnew']['subject'],
          'nid' => $fv['addnew']['nid'],
          'sid' => $fv['addnew']['sid'],
          'commenter_user_id' => $fv['addnew']['commenter_user_id'],
          'comment_parent' =>  '0',
          'ts' => date("Y-m-d H:i:s")
          ))->execute();
        if($insert1)
        {
            global $user; 
            $params = array(
               'body' => $message,
               'subject' => 'Website Information Request',
               'headers'=>'simple',
            );
            $message = drupal_mail('custom_webform_comments', 'custom_webform_comments_email', $user->mail, language_default(), $params, 'test@example.com', TRUE);
            if (!empty($message['result'])) {
                watchdog('mail', 'Mail sent Successfully (from %from to %to) for comment update', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_NOTICE);
                //drupal_set_message("Mail sent!");
            }
            else{
                watchdog('mail', 'Error sending e-mail (from %from to %to).', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_ERROR);
                drupal_set_message(t('Unable to send e-mail. Contact the site administrator if the problem persists.'), 'error');
            }
        }
        return $insert1; 
    }
    

    Similarly to get mail on every status update, update the following function.

    function custom_webform_comments_status_dbupdater($data) {
      global $user;
      //Insert values into database
      $insert = db_insert('custom_webform_submission_status')
        ->fields(array(
          'id' => NULL,
          'status' => $data['status'],
          'submit_time' => date("Y-m-d H:i:s"),
          'nid' => $data['nid'],
          'sid' => $data['sid'],
          'user' => serialize($user)
        ))->execute();
        if($insert)
        {
            global $user; 
            $params = array(
               'body' => $message,
               'subject' => 'Website Information Request',
               'headers'=>'simple',
            );
            $message = drupal_mail('custom_webform_comments', 'custom_webform_comments_email', $user->mail, language_default(), $params, 'test@example.com', TRUE);
            if (!empty($message['result'])) {
                watchdog('mail', 'Mail sent Successfully (from %from to %to) for status update', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_NOTICE);
                //drupal_set_message("Mail sent!");
            }
            else{
                watchdog('mail', 'Error sending e-mail (from %from to %to).', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_ERROR);
                drupal_set_message(t('Unable to send e-mail. Contact the site administrator if the problem persists.'), 'error');
            }
        }
      return $insert;
    }