tags:

views:

124

answers:

2

Hi All,

I'm using Drupal autoresponder module - and I want to use tokens so I can include the username who has subscribed within the emails being sent...

Does anybody know how this can be achieved?

Thanks for any help.

Shane

+3  A: 

EDIT after separate answer by OP: The following was based on the assumption that the $u->uid refers to the 'standard' Drupal user id, which is not correct! So one has to find the corresponding Drupal user by other means, if possible - see the OPs answer for details on this...


I have not tested it, but looking at the autoresponder source code, you should be able to add (user) token replacement in the autoresponder_mail() function by inserting the following code before the preparation of the plain text body (before line 392 in the 6.x-1.0-alpha1 release):

// load the full user object
$user = user_load($u->uid);
// Replace user tokens in mail body
$mail->body = token_replace($mail->body, 'user', $user);

Alternatively, you could do it one function call earlier in autoresponder_cron(), within the while ($message db_fetch_object($result_messages)) loop, before the if (autoresponder_mail($u, $message)) call (line 366), using $message instead of $mail:

// load the full user object
$user = user_load($u->uid);
// Replace user tokens in mail body
$message->body = token_replace($message->body, 'user', $user);

In case this works, you might want to submit it as a patch to the already existing feature request for this. (I guess you are the 'keyzo'/'shane' who already answered there ;)

If it works and you are going to create a patch, it would be 'standard' practice to add the hint on possible token replacement to the message definition form(s) in autoresponder_mail_edit_create_form().

Henrik Opel
+4  A: 

Hi Henrik,

Thanks for that - your answer was very close....

The autoresponder module UID is not related to the user UID so your code was bringing in a different username... So I changed it to find the user from the email address instead.

// load the full user object
$user = user_load(array('mail' => $u->mail));
// Replace user tokens in mail body
$mail->body = token_replace($mail->body, 'user', $user);

Yes indeed, I'll submit it as a patch to my other request, and hopefully it may help somebody else.

Many Thanks

Shane

Shane
+1 for follow up - I did not look at the schema and just assumed the 'standard' uid usage :/
Henrik Opel
On question/remark, though: Are anonymous users allowed to subscribe as well? This would be a good reason for the separate 'autoresponder_users' table, and it would mean that you'd have to add a check to your 'user lookup by mail' in case no corresponding Drupal user can be found. (`user_load()` would return FALSE in that case.)
Henrik Opel
Hi Henrik,Yeah appreciate about the schema - you pointed me in the right direction though :-) And I value your point about anonymous users - but I'd pre thought that to, so it's only for users that have an account, good thinking though!Thanks for your help.Shane
Shane