在 hook_mail() 中使用动态电子邮件生成
是否可以在 PHP switch
语句中动态生成 cases
,如 hook_mail()
示例中所示?
/*
* Implement hook_mail().
*/
function rsvp_mail($key, &$message, $params) {
$guests = rsvp_query();
foreach ($guests as $account) {
switch($key) {
case "invite $account->uid" :
$message['subject'] = "And invitation for $account->name";
$message['body'][] = 'Some body text.';
$message['body'][] = rsvp_get_link($account);
break;
}
}
}
Is it possible to dynamically generate cases
in PHP switch
statements, as in this example of hook_mail()
?
/*
* Implement hook_mail().
*/
function rsvp_mail($key, &$message, $params) {
$guests = rsvp_query();
foreach ($guests as $account) {
switch($key) {
case "invite $account->uid" :
$message['subject'] = "And invitation for $account->name";
$message['body'][] = 'Some body text.';
$message['body'][] = rsvp_get_link($account);
break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您对要发送邮件的所有用户使用相同的基本电子邮件“骨架”,并且您只需要替换一些特定于用户的值(用户名、个人资料信息等),则可以使用令牌来完成工作。
看一下这两个函数就明白我的意思了(它们并排在同一个文件中):
https://github.com/semperos/drupal-simple-subs/blob/master/simple_subs.module#L180
https://github.com/semperos/drupal-simple-subs/blob/master/ simple_subs.module#L191
当您使用
drupal_mail()
时,您可以将任何您想要的值分配给传递给它的params
数组(这是与传递给您的hook_mail
实现相同的params
数组)。然后可以将这些值添加到 Token 模块提供的开箱即用的默认邮件令牌中,如您在第 195-198 行的hook_mail
实现中看到的那样。如果您需要更复杂的内容(即电子邮件的主要文本取决于用户),您仍然可以使用相同的系统,但可以调用数据库以获取更多信息。如果您需要为每个用户存储不同的电子邮件正文,请将其存储在与用户 ID 关联的数据库表中,然后在邮件函数中动态查询该数据。
无论如何,您不必在 PHP 的
switch
语句级别使用任何类型的动态行为;您应该能够通过您的hook_mail
实现所期望的params
数组传入您需要的任何动态值。一般来说,用于处理邮件的case
语句并不意味着在每个用户或每个节点的基础上提供动态行为,而是用于管理具有截然不同的内容和用途的电子邮件。If you are using the same basic email "skeleton" for all the users you're mailing and you just need to replace a few values to be user-specific (user name, profile information, etc.), you can just use tokens to get the job done.
Take a look at these two functions to see what I mean (they're side-by-side in the same file):
https://github.com/semperos/drupal-simple-subs/blob/master/simple_subs.module#L180
https://github.com/semperos/drupal-simple-subs/blob/master/simple_subs.module#L191
When you use
drupal_mail()
, you can assign whatever values you want to theparams
array that you pass to it (this is the sameparams
array that gets passed to your implementation ofhook_mail
). Those values can then be added to the default mail tokens that the Token module provides out-of-the-box, as you see in my implementation ofhook_mail
on lines 195-198.If you need something more complex (i.e. the primary text of your email depends on the user), you can still use this same system, but make calls to the database for more information. If you need to store a different email body per user, store that in a database table associated with the user's id, then in your mail functions query for that data dynamically.
In any event, you shouldn't have to use any kind of dynamic behavior at the level of PHP's
switch
statement; you should be able to pass in whatever dynamic values you need via theparams
array that your implementation ofhook_mail
expects. In general, thecase
statements for dealing with mail are not meant to provide dynamic behavior on a per-user or per-node basis, but rather for managing emails with very different content and uses.