“ user:one-time-login-url”令牌未替换为token_replace


7

我正在尝试使用hook_mail_alter挂钩手动向用户发送电子邮件。以下是我的全部功能:

function custom_module_mail_alter(&$message) {
    $email = '[user:name],

A site administrator at [site:name] has created an account for you. You may now log in by clicking this link or copying and pasting it to your browser:

[user:one-time-login-url]

This link can only be used once to log in and will lead you to a page where you can set your password.

After setting your password, you will be able to log in at [site:login-url] in the future using:

username: [user:name]
password: Your password

--  [site:name] team';

    $account = $message['params']['account'];
    $uid = $account->uid;

    $_user = user_load($uid);

    dpm(token_replace($email, array('user'=>$_user)));
}

输出看起来像这样:

Peter,

A site administrator at Website has created an account for you. You may now log in by clicking this link or copying and pasting it to your browser:

[user:one-time-login-url]

This link can only be used once to log in and will lead you to a page where you can set your password.

After setting your password, you will be able to log in at http://localhost/website/user in the future using:

username: Peter
password: Your password

--  Website team

如您所见,诸如[user:name],[site:name]和[site:login-url]之类的内容已正确处理。唯一未处理的令牌是[user:one-time-login-url]。任何想法为什么会这样?

编辑:仅供参考,令牌确实会在系统自动发送的“欢迎”电子邮件得到处理,因此令牌模块处于活动状态并且正在工作...当我手动调用token_replace时,似乎并没有得到处理()。


您是否启用了token.module?看起来令牌是在token_token_info()中定义的,而不是在user_token_info()中定义的。
2011年

是的,已启用令牌模块,并且我正在使用最新的Beta7 ...这很奇怪。
彼得

Answers:


11

要更换令牌,你需要调用token_replace()token_replace($email, array('user' => $_user), array('callback' => 'user_mail_tokens', 'sanitize' => FALSE))

函数user_mail_tokens()在文档中描述为:

令牌回调,用于为用户邮件添加不安全的令牌。

此功能用于通过token_replace()在结束呼叫_user_mail_text()成立,可以在由所生成的电子邮件中使用一些附加的令牌()user_mail

_user_mail_text()以下是用于调用该回调的代码。

// We do not sanitize the token replacement, since the output of this
// replacement is intended for an e-mail message, not a web browser.
return token_replace($text, $variables, array('language' => $language, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE));

在Drupal 7中,令牌模块不是替换令牌所必需的。替换令牌的代码是Drupal 7核心代码的一部分。Drupal 7中的令牌模块定义了Drupal核心模块未定义的其他令牌。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.