通过hook_mail发送一次性登录链接

时间:2011-05-02 20:35:35

标签: php drupal drupal-modules drupal-7 drupal-hooks

这是我对“模块”的第一次真正尝试(我认识到这不适合回馈drupal社区 - 我只是试着让API去做我想做的事。)不幸的是,这不起作用,我不知道为什么。想知道是否有人对方法有任何意见或看到我的错误。

我的问题:  有没有更好的方法来做到这一点,所以我不必多次拨打rsvp_query()

这是一种合理的方法,可以为包含自定义链接的角色(或其他类)的所有用户递归生成电子邮件(在这种情况下基于散列的pw和时间戳?

<?php
function rsvp_menu() {
  $items['invite'] = array (
    'title' => 'Invite Guests',
    'page callback' => 'rsvp_invite',
    'access arguments' => array('administer content'), 
    // TODO - set proper perms - 
    // does this perm asffect the login_one_time link landing page?? 
  );
  $items['invite/send'] = array (
    'title' => 'Send Invitations',
    'page callback' => 'rsvp_send',
    'access arguments' => array('administer rsvp'),
    'type' => MENU_CALLBACK,
  );    
  $items['rsvp/%/%/%'] = array (
    'title' => 'RSVP',
    'page arguments' => array(1,2,3),
    'page callback' => 'rsvp_page',
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/*
 * Implement hook_mail().
 */

function rsvp_mail($key, &$message, $params) {
  $guests = rsvp_query();
  foreach ($guests as $account) {
    switch($key) {
      case "invite $account->name" :
        $message['subject'] = 'A different kind of invitation';
        $message['body'][] = 'Some bloody body text.';
        $message['body'][] = rsvp_get_link($account);
        break;
    }
  }
}



function rsvp_mail_send($guests) {
  global $user;
  foreach ($guests as $account) {
    $module = 'rsvp';
    $from = $user->mail;
    $key = "invite $account->name";
    $to = $account->mail;
    $language = language_default();
    $send = TRUE;
    $params = array();
    $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
    if ($result['result'] == 1) {
      $verify[] = "Mail to $account->name at $account->mail succesfull";
    } else {
      $verify[] = "Mail to $account->name at $account->mail NOT succesfull";
    }
  }

 return $verify;  //This doesn't work.
}

/**
 * Return array of guests as user objects.
 */
function rsvp_query() {
  $result = db_query('SELECT uid FROM {users_roles} WHERE rid = :rid', array(':rid' => 4));
  foreach ($result as $row) {
    $guests[] = user_load($row->uid);
  }  
  return $guests;
}

/* menu callback */
function rsvp_invite() {
  $guests = rsvp_query();
  foreach ($guests as $guest) {
    $item[] = $guest->name;
  }
  $vars = array(
    'items' => $item,//$guests,
    'title' => 'The following users have not received invitations',
    'type' => 'ul',
    'attributes' => array('class' => 'list-to-send'),
  );
  $output = theme('item_list',$vars);
  $output .= l('Send Invites', 'invite/send');
  return $output;
  }

/* menu send */
function rsvp_send() {
  $guests = rsvp_query();
  $mail = rsvp_mail_send($guests);
  return $mail;
 }

/* generate info for one-time login link */
function rsvp_get_link($account) {

  $path = "user/$account->uid/edit/Wedding";

  $timestamp = REQUEST_TIME;

  return url("rsvp/" . $account->uid . "/" . $timestamp . "/" .
    md5($account->pass . $timestamp) . "/" . $path, array('absolute' => TRUE));
}

/* TODO rsvp callback */

同样在http://pastebin.com/594wkHWs

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

$params = $guest
$result = drupal_mail($module, $key, $to, $language, $params, $from, $send);

使用它,而不是$ params = array()。然后在hook_mail中,$ params将= $ guest变量。 Hook_mail将为每位客人点火一次。