2011-05-02 136 views
1

一次性登錄鏈接這是一個「模塊」我第一次真正嘗試(我承認,這是不是適合貢獻回到Drupal社區 - 我只是想在我的手得到API來做我想做的事。)不幸的是,這不起作用,我不知道爲什麼。不知道有人對方法有任何意見或看到我的錯誤。發送通過hook_mail

我的問題: 有沒有更好的方法來做到這一點,所以我不必多次撥打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

任何幫助不勝感激。

+0

您可以通過當前客戶從rsvp_mail_send $ params數組來rsvp_mail英寸這正是它的參數:) – yoavmatchulsky 2011-05-03 06:18:46

回答

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

使用,而不是$ PARAMS =陣列(),然後在hook_mail,$ PARAMS將所述= $ guest變量。 Hook_mail將爲每位客人啓動一次。

+0

實際上,它更好的$ params是一個數組,所以使用$ params ['guest'] = $ guest – yoavmatchulsky 2011-05-04 07:32:28