Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal Autoresponder with Token Module

Tags:

php

token

drupal

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

like image 235
Shane Avatar asked Jun 13 '10 15:06

Shane


2 Answers

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

like image 171
Shane Avatar answered Nov 03 '22 22:11

Shane


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().

like image 26
Henrik Opel Avatar answered Nov 03 '22 22:11

Henrik Opel