Send one-time login link via hook_mail
-
28-10-2019 - |
문제
This is my first real attempt at a "module" (I recognize that this isn't something fit for contribution back to the drupal community - I'm just trying my hand at getting the APIs to do what I want.) Unfortunately, this isn't working and I'm not sure why. Wonder if anyone has any input on the approach or sees my error(s).
My questions:
Is there a better way to do this so I don't have to call rsvp_query()
more than once?
Is this a sensible approach to recursively generating an email for all users of a role (or other class) containing a customized link (in this case based on a hashed pw and timestamp?
<?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 */
Also at http://pastebin.com/594wkHWs
Any help is greatly appreciated.
해결책
$params = $guest
$result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
Use that, not $params = array(). Then in hook_mail, $params will = the $guest variable. Hook_mail will fire once per guest.