posts - 431,  comments - 344,  trackbacks - 0

在使用action和workflow时候,突然想到怎么给一个角色组里的所以人发email,然后在主站上找到了一篇文章介绍怎么实现http://drupal.org/node/48738,添加以后,按照步骤,进入action设置那里添加所要的功能,然后设置发送的角色组。这时候突然想到之前实现的日历事件,然后就决定实现这样一个效果,我添加一个事件,例如添加一个会议通知,然后通过email形式发送到角色组里的成员邮件里,这样就要去workflow里面创建一个工作流,添加action,然后把这工作流添加到event 的node type上!这样就可以了!
要在action.module里面添加如下的代码:

function action_send_email_torolegroup($op, $edit = array(), $node) {
  switch($op) {
    case 'metadata':
      return array(
        'description' => t('Send Email to a role group'),
        'type' => t('Email'),
        'batchable' => false,
        'configurable' => true,
      );

    case 'do':
      // note this is the user who owns the node, not global $user
      $user = user_load(array('uid' => $node->uid));
      $site_name = variable_get('site_name', 'Drupal');
      $from = "$site_name <" . variable_get('site_mail', ini_get('sendmail_from')) . '>';
      $subject = $edit['subject'];
      $message = $edit['message'];
     
      foreach($edit['recipients'] as $rid => $chose_role) {
        if ($chose_role) {
          $recipient_roles[] = $rid;
        }
       
      }
     
      $recipient_addresses = array();
     
      if ($recipient_roles[2]) { //email every one! wow. that's why a watchdog is invoked

        watchdog('action', t('Sent an email to every single registered user. Use with caution.'));
       
        $result = db_query('SELECT mail FROM {users} WHERE uid > 0');
        while($account = db_fetch_object($result)) {
          $recipient_addresses[] = $account->mail;
        }
      } else {
        $roles = implode(',', $recipient_roles);   
        $result = db_query('SELECT DISTINCT u.mail FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN (%s)', $roles);
        while($account = db_fetch_object($result)) {
          $recipient_addresses[] = $account->mail;
  
        }
      }
     
      if (isset($node) && is_object($node)) {
        $variables = array(
          '%site_name' => $site_name,
          '%username' => $user->name,
          '%uid' => $node->uid,
          '%node_url' => url('node/' . $node->nid, NULL, NULL, TRUE),
          '%node_type' => $node->type,
          '%title' => $node->title,
          '%teaser' => strip_tags($node->teaser),
          '%body' => strip_tags($node->body)
           );

        $message = strtr($message, $variables);
      }
      foreach ($recipient_addresses as $recipient) {
        if (user_mail($recipient, $subject, $message, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from" )) {
          watchdog('action', t('Sent email to %recipient', array('%recipient' => $recipient)));
        }
        else {
          watchdog('error', t('Unable to send email to %recipient', array('%recipient' => $recipient)));
        }
      }
      break;

    // return an HTML config form for the action
    case 'form':
      // default values for form
      //  if (!isset($edit['recipients'])) $edit['recipients'] = '';
      if (!isset($edit['subject'])) $edit['subject'] = '';
      if (!isset($edit['message'])) $edit['message'] = '';
      $form = array();
      $roles = user_roles();
      unset($roles[1]); // good bye anonymous users!
      //unset($roles[2]); // good bye authenticated users!
           
      $form['recipients'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Recipient Role Groups'),
        '#default_value' => $edit['recipients'],
        '#options' => $roles,
        '#description' => t('Select which roles should receive this email.'),
    );
     
      $form['subject'] = array(
        '#type' => 'textfield',
        '#title' => t('Subject'),
        '#default_value' => $edit['subject'],
        '#size' => '20',
        '#maxlength' => '254',
        '#description' => t('The subject of the message.'),
      );
      $form['message'] = array(
        '#type' => 'textarea',
        '#title' => t('Message'),
        '#default_value' => $edit['message'],
        '#cols' => '80',
        '#rows' => '20',
        '#description' => t('The message that should be sent.  You may include the following variables: %site_name, %username, %node_url, %node_type, %title, %teaser, %body'),
      );
      return $form;

     // validate the HTML form
    case 'validate':
      $errors = array();
     
      $roleselected = false;
      foreach($edit['recipients'] as $rid => $selected) {
        if ($selected) {
          $roleselected = true;
        }
      }
      if (!$roleselected) {
        $errors['recipients'] = t('Please enter at least one recipient role group');
      }
     
      foreach ($errors as $name => $message) {
        form_set_error($name, $message);
      }

      return count($errors) == 0;

    // process the HTML form to store configuration
    case 'submit':

      $params = array(
        'recipients' => $edit['recipients'],
        'subject'   => $edit['subject'],
        'message'   => $edit['message']);
      return $params;
  }
}

/**
* Send an e-mail message.
*/
function user_mail($mail, $subject, $message, $header) {
  if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) {
    include_once variable_get('smtp_library', '');
    return user_mail_wrapper($mail, $subject, $message, $header);
  }
  else {
    return mail(
      check_email_header($mail),
      mime_header_encode(check_email_header($subject)),
      str_replace("\r", '', $message),
      "MIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8; format=flowed\nContent-transfer-encoding: 8Bit\n" . check_email_header($header)
    );
  }
}

function check_email_header($val)
{
   if (strpos($value, "\r") !== false || strpos($value, "\n") !== false)
   {
     die("Why ?? :(");
   }

   return $val;
}

posted on 2007-11-21 19:14 周锐 阅读(263) 评论(0)  编辑  收藏 所属分类: PHP

只有注册用户登录后才能发表评论。


网站导航: