Send tokenized email to members of a role
Steven Merrill - November 2, 2008 - 23:42
| Project: | Token |
| Version: | 6.x-1.12 |
| Component: | Token Actions |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Steven Merrill |
| Status: | needs work |
Description
In building a lot of custom workflows, I've found that the ability to send an email to all users of a role would be useful, so here's a patch against 6.x-1.11 which provides a new "Send a tokenized email to users of a role..." action. It's got fairly robust error checking.
Enjoy!
| Attachment | Size |
|---|---|
| new_role_email_action.patch | 5.26 KB |

#1
This patch adds one small but desirable bit over the last one - a #default_value for the form, so that the value isn't eradicated upon going back and editing the advanced action.
#2
This seems pretty handy to me. Two questions:
1) The style of the recipient query seems weird:
$recipient_query = <<<QUERY+SELECT u.mail FROM {users} u
+INNER JOIN {users_roles} ur ON ur.uid = u.uid
+WHERE ur.rid = %d
+QUERY;
I don't think I've ever seen a query like that in Drupal. More common seems to be one big long line which is what I'd prefer.
2) I think it would be better to put the drupal_mail inside of the for loop and send one e-mail per person in the role. First, that makes the watchdog error more specific. Second (and I haven't tested this, soo...) I'm assuming that putting all the recipients on the to: line will mean that all members of a role will see the e-mail addresses for that role. For privacy reasons it's important we don't let that happen.
Sending mails in a tight loop like that can be a performance issue if the role is really big, but we have solutions like http://drupal.org/project/queue_mail for that.
#3
Will this functionality be added to token actions? It seems very usefull to me. This project also does role based email actions, only it doesn't use token.
#4
Subscribe
#5
subscribe as well. I can clean up this patch if wed like to push it forward. Using email acton role until it is mainlined
#6
I think it's just missing a code-style re-roll. Please do re-roll/test and I'll test/commit.
#7
very interested in this too
#8
I fully agree with 'greggles' 2nd remark that:
2a) for monitoring/ maintenance purposes, each email should be send separately (so logging/ watchdog errors become more specific).
2b) for privacy reasons recipients should NOT be able to determine to which other users the tokenized email is send (let alone expose their email addresses).
As this feature request is applicable to any current version and in order to prevent duplicate submissions of this feature request, I changed the version to the latest currently available. Please correct me, if the best practice in such cases is different.
#9
I agree -- it would be nice to have a choice between (A) all recipients in the To: field, and (B) private recipients, either by sending separate emails or by using the Bcc field. But to have only (A) is a privacy problem.
In this post #267921: All recipients visible on outgoing mails, jeff gives us a patch for module Action email role that (I think) provides a check box in Action|configure to make this choice.
BUT, he states "Instead of multiple calls to drupal_mail()..." -- Token.module appears to call drupal_mail() with a list of recipients it extracted from the role. By inspecting drupal_mail() and drupal_mail_send() I see that it's up to the module coder to call drupal_mail separately for each recipient (too bad 'private' isn't an available parameter!).
This is an important topic for me, so if necessary I'll find someone to patch Token; I'd just need a consensus on how to proceed.
#10
What about bcc? I've never done it, but can't Drupal send using one call to drupal_mail and bcc to achieve "private"?
#11
I agree that Bcc might be best when sending to a role, but drupal_mail() seems to not have a bcc field. From the API site:
6-7 drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE)and in mail.inc:
// Bundle up the variables into a structured array for altering.$message = array(
'id' => $module .'_'. $key,
'to' => $to,
'from' => isset($from) ? $from : $default_from,
'language' => $language,
'params' => $params,
'subject' => '',
'body' => array()
);
Does anyone think a bcc field should be added to mail.inc and drupal_mail()? (I also don't see any checks against an empty To: field)
'bcc' => $bcc,BUT, also in drupal_mail() is this section that appears to let a module alter the email via $params. Since the idea above seems crude and would require a drupal core change, perhaps the road to Oz is below (please pardon my newbiness, I'm still learning drupal, PHP and coding methodology)
// Build the e-mail (get subject and body, allow additional headers) by
// invoking hook_mail() on this module. We cannot use module_invoke() as
// we need to have $message by reference in hook_mail().
if (function_exists($function = $module .'_mail')) {
$function($key, $message, $params);
}
// Invoke hook_mail_alter() to allow all modules to alter the resulting e-mail.
drupal_alter('mail', $message);
What would this require? Something in token_actions_mail() that replaces "TO:" with "BCC:" in $message if, say, "private_bcc" is specified in $params? (???)
EDIT: I just noticed this function's already here, but I'm puzzled how to attack this. Do I add somewhere the code to set $params['use_bcc'] = 1 and then in this function check with an IF, and replace all the TO:'s to BCC: in $message['header']?
I did a temp hack on token.module to explode $recipient and then run foreach to send individual emails (yes, a performance hit on large roles, but my user base is still small.)
I'd be nice to also integrate similar code to what jeff.ferrari (awesome name) did for Action email role (#267921: All recipients visible on outgoing mails), such as have a radio button choice when defining the tokenized email action.
#12
I'm testing a patch that creates a form checkbox that forces token_actions to send by Bcc: rather than To:. It also checks each user's status and skips them if blocked.
Should I open a "code" or "Token Actions" feature request of type 'needs review' or just attach the patch here?
Note -- I still have two problems...
1) What to put in the TO field. Should it be NO-REPLY@mydomain.com?
2) This is for announcing new-posts. But what if some users don't want to be notified? Will I need a user-page that lets them flag "don't email me on new posts" in the database? (ps, I don't allow access to the 'My account' page)
#13
@mudd - Thanks for your work. Please open a new issue for this.
#14
What's the current state of this issue, is this going to be released soon or are you still working on it?
EDIT: I really need this feature, I might help if you want.
#15
A small patch for BCC is here: #513934: BCC email option for Token Actions
#16
subscribing
#17
subscribing
#18
Did anyone else notice that the node tokens don't get tokenized?
I'm using the "Send tokenized e-mail to users of a role" for the event "After saving new content" with the following tokens, none of which get replaced:
[type-name]
[title]
[nid]
#19
I'm too lazy to roll a patch, but adding this on the first line of
function token_actions_send_email_to_role_actionseems to have fixed it:if(empty($context['node'])) {$context['node'] = $object;
}