【问题标题】:Drupal 7 - Send an E-mail to user on content modificationDrupal 7 - 向用户发送有关内容修改的电子邮件
【发布时间】:2018-02-20 15:34:07
【问题描述】:

我有一个网络表单页面,用户可以在其中提交查询。在提交表单时,会向用户和管理员发送一封确认邮件。我已经安装了Custom Webform Comment 模块,用户和管理员都可以通过该模块评论和更改表单中的状态,现在我想将每个评论和状态更改的邮件发送给管理员和用户。

我尝试过使用Webform Rules 模块,但它不起作用。模块没有发送邮件

我可以通过其他方式或模块向用户发送邮件吗?

网络表单版本 7.x-4.15

提前致谢:)

【问题讨论】:

    标签: drupal-7 drupal-webform drupal-rules


    【解决方案1】:

    您可以在自定义表单中使用 hook_webform_submission_update 来发送电子邮件,或者您可以为此使用操作和触发器

    1. 为此,您必须添加新的自定义模块,您可以参考此链接:- https://www.drupal.org/docs/7/creating-custom-modules/getting-started

    2. 在您的 custom.module 文件中添加此代码

    参考此代码

     function <moduleName>_webform_submission_update($node, $submission)
            {
                            // you can send mail like this
                            $message = 'New signup email address'; // Body of your email here. 
                            //you can also set any form detail using $node,$submission variable
                            $params = array(
                                   'body' => $message,
                                   'subject' => 'Website Information Request',
                                   'headers'=>'simple',
                             );
                             $to = "Your Email Address";
                            drupal_mail('contactform', 'send_link', $to, language_default(), $params, 'demo@demo.com', TRUE);
    
             }
    

    【讨论】:

    • 感谢您的回复...但是请您详细说明一下,因为我是新来的钩子
    【解决方案2】:

    我通过在 Custom Webform Comment 模块中插入 hook_mail 代码解决了上述问题。

    如果其他人遇到同样的问题,这里是我在 custom_webform_cmets.module 文件中插入的代码。

    /**
     * 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;
      }
    }
    

    在上述代码行之后,将以下代码放在函数custom_webform_cmets_commentform_submit() 中的数据库插入查询之后,以便在每次评论更新后发送邮件。

    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; 
    }
    

    类似每次状态更新时获取邮件,更新如下函数。

    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;
    }
    

    【讨论】:

    • 最好使用自己的自定义模块,而不是编辑贡献的模块。您可能会在自定义模块更新时遇到问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多