Posted by inverarityp on October 27, 2006 at 11:53pm
I've set up a number of custom fields in user profiles using the profile module, and I've successfully added them to the user registration page. My question is whether I can somehow access those within the welcome email?
It would be easy to implement if there were an 'input format' option associated with the welcome email text fields in administer>>settings>>user. (Then you could use php.) However, it seems that the email message is only allowed to use those few predefined variables.
Any ideas?
Thanks.
Comments
I add my voice...
... to this plea. I would very much like to add such fields into my welcome email myself. Can't anybody help us?
Thanks,
Frank
I agree
This topic is very old at this stage but I would also like to see this happen.
Thank you
Sean
You can build a simple
You can build a simple custom module and use hook_mail_alter to add custom data to mailings:
http://api.drupal.org/api/function/hook_mail_alter/6
my fix for this
old thread at this point but i thought i'd share how i did this for anyone just searching for the topic (as I was about 3 hours ago). maybe i'll turn this into a module at some point but for now it's an edit to the user.module file. I'm running 6.x.
in the vicinity of line 2075 you'll find:
<?php$tokens = array(
'!username' => $account->name,
'!site' => variable_get('site_name', 'Drupal'),
'!login_url' => user_pass_reset_url($account),
'!uri' => $base_url,
'!uri_brief' => preg_replace('!^https?://!', '', $base_url),
'!mailto' => $account->mail,
'!date' => format_date(time(), 'medium', '', NULL, $language->language),
'!login_uri' => url('user', array('absolute' => TRUE, 'language' => $language)),
'!edit_uri' => url('user/'. $account->uid .'/edit', array('absolute' => TRUE, 'language' => $language)),
);
?>
add
$user = user_load(array('uid'=>$account->uid));just before the $tokens variable and append'![variablename]' => $user->profile_[customvariable],to the end of the $tokens variable where [variablename] is anything you like and profile_[customvariable] is the variable name of the custom profile field you want to use. you should now have:<?php$user = user_load(array('uid'=>$account->uid));
$tokens = array(
'!username' => $account->name,
'!site' => variable_get('site_name', 'Drupal'),
'!login_url' => user_pass_reset_url($account),
'!uri' => $base_url,
'!uri_brief' => preg_replace('!^https?://!', '', $base_url),
'!mailto' => $account->mail,
'!date' => format_date(time(), 'medium', '', NULL, $language->language),
'!login_uri' => url('user', array('absolute' => TRUE, 'language' => $language)),
'!edit_uri' => url('user/'. $account->uid .'/edit', array('absolute' => TRUE, 'language' => $language)),
'![variablename]' => $user->profile_[customvariable],
);
?>
you can now use ![variablename] just like you would !site, !username, or any of the other email variables. for emails that aren't customizable in admin, like the registration-pending-approval email that gets sent to the site email address, you can change the copy in the switch statement around line 1647ish.
You're awesome.
It's funny how it seems someone's already just dealt with an issue as you're searching it out. Thanks wickedpixel!
I'll be doing this tomorrow, and I'll try to add anything that seems relevant :)
HIGHFIVE FOR OPEN SOURCE COMMUNITIES.
mmmmMMAAAD Props!!!
wickedpixel...
Thank you so much!
Do not hack core
Hello,
Even though wickedpixel's approach actually works, it is not a recommended approach.
See: http://drupal.org/best-practices/do-not-hack-core
The approach of kjl: using hook_mail_alter() is better.
--
Drupal Freelancer (Module Developer)
Christian Roy
Custom module OR Rules module
For the suggestion of using hook_mail_alter() in a custom module:
As far as I can figure out, at that point, you don't have access to the registered user account anymore, which was passed with _user_mail_notify() but not anymore into drupal_mail(), which makes it impossible to inject our profile as we don't have a basis for our UID.
One solution could be loading the user object again at that point, based on the mailto address (which should be unique for this user).
Another possibility is using the Rules module, which has a trigger "When user account is created". On that trigger you can add the Action "System -> send mail to user" and toggle to "Registered user" (as you can also create users as an admin, and then you are the acting user, not good!
In that email action, you have practically all forementioned options, such as PHP code (with a loaded $account object containing your profile data, or at least a UID to load the profile).
Greets,
Kim
Just wanted to chime in and
Just wanted to chime in and agree with the "don't hack core" sentiment. I was youthful and naive when I first got excited about this approach (although I still do appreciate that someone was willing to help :)