Support Token module
pixelcort - October 13, 2007 - 00:27
| Project: | Simplenews |
| Version: | HEAD |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Description
It would be nice to include in a newsletter something like:
Hello, {profile_firstname}!
This would then pull from the proflie of each user their firstname and put it in for each personalized newsletter sent out.

#1
This could be achieved with an integration of Token module with simplenews. Patches are welcome.
#2
Duplicate request: #364408: Token integration
#3
I had this half finished simplenews.token.inc module in my sandbox. Perhaps someone wants to take if further. Using the recently added function simplenews_mail_tokens() it should not be too hard.
#4
I would also suggest that the Token module is used instead of the SImlenews specific tokens
%site,%uri,%uri_brief,%mymail,%date,%login_uri.#5
I couldn't agree with you more.
#6
Duplicated by: #400936: Utilize Tokens
#7
Changing version to HEAD. That's were the action is ;)
#8
Duplicated by #87071: Personalizing newsletters. Contains D5 patches.
#9
subscribing
#10
If somebody want quick custom variables, you can add your own function in:
<?php
function simplenews_mail_tokens($subscription, $context, $language) {
...
if (function_exists('my_module_get_mail_tokens') ) {
$vars = array_merge($vars,my_module_get_mail_tokens());
}
return $vars;
}
?>
Then you can use your own function:
<?php
function my_module_get_mail_tokens() {
global $user;
$tokens = array(
'!user_name' => $user->name,
'!something' => 'bla bla bla',
);
return $tokens;
?>
#11
I am very interested in your solution for personalization of newsletters.
Could you tell where to put your functions and how to use them with simplenews, please.
Thanks
#12
Im new on Drupal.org but i am interested in changing the simplenews_mail_tokens into tokens from the token module like Sutharsan wrote in his third post.
In the next days i write how the development is going on.
#14
Applying this patch allows you to use !recipient_name in your mail or template.
If the mail is sent out to an anonymous subscriber the email is inserted instead.
#15
@Pboenisch: any progress on your development?
@Eikaa: this is perhaps a nice intermediate solution, but implementing Token module is not about adding an individual place holder. Token gives much, much more options that just recipent name.
#16
@Sutharsan: It is in work. I am still making the last tests with the tokens. At the moment it looks well and i think i can upload it in a few days
#17
You can use mail_alter hook for define some custom tokens:
Example:
<?php/**
* Implementation of hook_mail_alter
*/
function mymodule_mail_alter(&$data) {
if (isset($data['params'])) {
$var = array ('!my_custom_variable' => 'my variable data');
$data['body']['body'] = strtr($data['body']['body'], $var);
}
}
?>
#18
@Pboenisch I would love to test your new token code when you're finished.
#19
Simpler solution:
<?php/**
* Implementation of hook_mail_alter().
*
*/
function hook_mail_alter(&$message) {
/* detect mail body format */
if (is_array($message['body'])) {
$body = &$message['body']['body'];
} else {
$body = &$message['body'];
}
$body = token_replace_multiple($body); /* replace all available tokens */
}
?>
Define this hook to replace all tokens available in body mail.
Eventually you can add some condition if Token module is enabled.
#20
subscribing
#21
Hi there
i'm sorry that you must wait so long for this patch. I didn't had the time for this because i wrote my bachelor thesis.
Here is my patch for the simplenews 6.x-1.0-rc6. I removed the simplenews_mail_tokens function and added the well-known token_list and token_values functions.
Please test it and give me feedback.
#22
I would love to test this out but it fails against current HEAD, can you re-roll please?
In the meantime I'll apply it to an earlier revision and check it out.
Thanks,
John
#23
Patched the failed hunks manually and it all seems to be working. I'm also adding my own tokens to insert a content listing view based upon the user's profile preferences. Fantastic, thanks for this!
#24
This is a patch for current HEAD for those who also want to test and report their results.
#25
I had a better look at Phoebisch's patch. It failed and I had to completely overhaul the code. A new patch (for HEAD) for review is attached.
#26
This is probably not needed... It should be fine to make it an "optional dependency" just like with mailmime.
I can't tell if it would work with this patch or not, but I'm definitely going to need access to $user->profile_company, etc. (ability to pull in user profile fields and map them to a token usable in the send, even if it requires a hook in a custom module).
Nothing else jumps out at me, I'll have to see how it works in practice.
#27
Tokens are also used for the confirmation emails. This makes token a requirement. To use this part without tokens would mean that simplenews requires two systems of token replacement. Token is used in 50% of the drupal 6 installations and is the third most used module. I don't see a real problem in making simplenews dependent on token.
#28
I've checked the patch and found two issues:
1. All tokens where displayed in admin for mail replacement. Added token_help specifier. I home this is correct behaviour.
2. Footer .tpl.php tokenized update was missing.
3. Removed unused obsolete email_token_help in .admin
Everything else unchanged.
In general i think it works pretty well, although i've found a bunch of small errors to fix outside of the scope of this issue.
#29
Thanks for your review. I leave the issue open for a little longer to get another review but for me the last patch is already RTBC worthy.
#30
Setting to RTBC. Checked another couple of minutes and reproducing code and especially checking static caching not to cache things that shouldn't be cached...
Seems perfectly right. Cool patch, Sutharsan!
Next step in evolution is:
#581500: Conditional personalisation and dynamic (uncacheable) filters
#31
We need a note for users who upgrade to make clear they need to rewrite their mailings.
If not they will result having broken obsolete replacement patterns inside mails.
#32
I've added a replace function for the confirmation messages and a user message.
Attached patch is committed to HEAD. Thanks to all for encouragement, code and test.
#33
Very nice! Thanks a lot.
#34
Automatically closed -- issue fixed for 2 weeks with no activity.
#35
Hi, I have created simplenews_token module to integrate simplenews and token module. I developed it for drupal 6. What do you think?
#36
I'd rather combine efforts for this function. Please check-out simplenews HEAD, review and supply patches for it.
#37
Hi -- thanks for the module. Call me a newbie loser but I haven't quite nailed patch installation, so I was struggling with this. Having a module was great, I could use it right away. But, I don't know if it does what I need.
Problem: I used the global tokens listed at /admin/store/help/tokens in my newsletter, and they mostly worked great. However, the [user-name] token gives the name of the currently logged in user, which is no help to me in a newsletter because all the newsletters wind up being addressed to "admin" or "bob" or whoever creates the newsletter.
Is there a token available to get the user name of the user receiving the email?
Thanks!
ETA --
aha, I see that there is a drop down menu in the newsletter edit screen titled "replacement patterns" with the user tokens. [user] works perfectly.
Thank you for making this available, I appreciate it!