New user notification

Permalink 13 users found helpful
This has been asked before, but I can't find a definitive answer. If I have registration set to manually approve, is there a way for c5 to send out an email notifying the admin a new user has registered? Thanks for the help!


-ADMIN EDIT:

This is now included in the core. Just upgrade and enjoy
-frz

hursey013
 
Mnkras replied on at Permalink Reply
Mnkras
sorry but there isn't at the time
dg1 replied on at Permalink Reply
I needed this also and ended up coding the following:

1. Create file config/site_events.php and add something like the following:
<?php
   Events::extend('on_user_add',             /* event */
                  'ApplicationUser',         /* class */ 
                  'setupUserJoinInfo',       /* method */
                  'models/application_user.php'); 
?>


2. Create the code in models/application_user.php. Here you can send the email to the admin for the new user as follows. Note that I also add the user to a group called unvalidated so the admin can find the users easily.

<?php 
class ApplicationUser extends Object {
  public function setupUserJoinInfo($ui)
  {  
     Loader::model('groups');
     $u = $ui->getUserObject(); 
     // add new user to groups
     $g1 = Group::getByName('unvalidated');
     if (is_object($g1)) { 
        $u->enterGroup($g1);
     }     
     // Notify admin that new user added
     $adminUserInfo=UserInfo::getByID(USER_SUPER_ID);
     $adminEmailAddress = $adminUserInfo->getUserEmail(); 
     $mh = Loader::helper('mail');


3. You need to create the mail template (in this example, create mail/notify_new_user_added.php). Look at an existing template in the concrete directory for an example.

4. You also need to edit config/site and add the following:
define('ENABLE_APPLICATION_EVENTS',true);
hursey013 replied on at Permalink Reply
hursey013
I'll give it a shot - thank you for your help!
geojlc replied on at Permalink Reply
geojlc
When I used this example, I was able to get an email when I created a new user, but I also got all kinds of "Warning: Cannot modify header information - headers already sent by (output started at..." errors throughout the dashboard.

Most of them were either for /home/c5host/msm_versions/042010/concrete/libraries/view.php

Events::fire('on_before_render', $this);
            if (defined('APP_CHARSET')) {
               header("Content-Type: text/html; charset=" . APP_CHARSET);
            }


or /home/c5host/msm_versions/042010/concrete/libraries/controller.php

public function redirect() {
      $args = func_get_args();
      $url = call_user_func_array(array('View', 'url'), $args);
      if ($url == '') {
         $url = BASE_URL . DIR_REL;
      }
      header("Location: " . $url);
      exit;
   }


I tried commenting out the add user to a group bit, and I tried breaking down the mail template to the simplest I could make it. I also tried doing some searching on the 'cannot modify header informatoin' without much luck. Unfortunately, I haven't done enough scripting to know where else to troubleshoot. As far as I can tell, I'm not trying to change headers, but I don't really understand what I'm doing either...
cyandesigns replied on at Permalink Reply
I set this up using the instructions above a week or so ago and found the same error messages.

On the two files that were created (site_events.php & application_user.php) I simply removed the ending ?> on both files and that cleared it up.
cursal replied on at Permalink Reply
cursal
That worked Great!

was a little confused about where to put the new mail template. I put it /blocks not in the concrete/mail.

But once I switched it and made note of what cyandesigns said about dropping the ?>

Thank you
cyandesigns replied on at Permalink Reply
The mail template should go into the /mail folder that's OUTSIDE the /concrete folder.
geojlc replied on at Permalink Reply
geojlc
Yea! taking out the end ?> also worked for me! Thank you for figuring this out!
andrew replied on at Permalink Reply
andrew
Nice work. We'll probably build in some basic logic to handle some notifications on signup in the next version (since it's pretty obvious and kind of an oversight that we don't have it already) but this is a great solution given what's currently there, if you don't mind working with some PHP - plus, it gives a lot more flexibility than what we're likely to build into the core.
c5mix replied on at Permalink Reply
dg1,
This works great! Do you know how I can automatically send an email to the new user once they've been activated by the admin?
cssninja replied on at Permalink Reply
cssninja
Thanks, this works great, but I have problem with notification template.
How I can send notification email to different user than Super Admin?
In email can I show a new user name?
dg1 replied on at Permalink Reply
You can send the notification email to anyone; I just chose to use SuperAdmin. If you look at the methods in concrete/models/userinfo.php, there is a getByUserName or a getByEmail function, so you could use one of those instead of the getByID that I used. The rest of the code should be the same.

Another option is to create a special group and then list the user(s) in that group as the ones to be notified. I haven't tried that though but I looked in models/groups.php and there is a getGroupMembers method you could call. Then the end user can change whoever gets the email without you having to change the code.
rdains replied on at Permalink Reply
rdains
@c5mix: To send an email to users upon activation, copy concrete/models/userinfo.php to /models/userinfo.php. After the function activate() code (around line 540), add this code:

//send user email on activation
           $userEmailAddress = $this->uEmail;
           $mh = Loader::helper('mail');
           $mh->addParameter('userName', $this->uName);
           $mh->to($userEmailAddress);
           $mh->load('user_activation');
           $mh->sendMail();


Then create a mail template called "user_activation.php" in the mail folder.

Now your users will receive an email each time you activate them.
peejay68 replied on at Permalink Reply
Thanks to the code in this thread I now receive a notification email. However, is it possible to include user attribute fields in the notification template?

E.g. Name, Email, Company Name etc.?
rdains replied on at Permalink Reply
rdains
You should be able to do this, but you'd have to edit the user_activation.php template to do so. You'll also have to add the helpers to the code above to pull the parameters from the database. Here's the link to the mail helper function:

http://www.concrete5.org/documentation/developers/helpers/mail...

Hope this helps.
chschubert replied on at Permalink Reply
chschubert
Do you find a solution for that problem?
benradler replied on at Permalink Reply
If you're like me, this post might've helped a lot -- however, I also needed my users to be added and removed from groups when their accounts are activated or deactivated.

To do this, copy userinfo.php to /models and add the following code to the activate() and deactivate() methods:

function activate() {
          Loader::model('groups');
           $u = $this->getUserObject(); 
           // update user's groups
           $g1 = Group::getByName('Approved');
           if (is_object($g1)) { 
              $u->enterGroup($g1);
           }        
           $g2 = Group::getByName('Unapproved');
           if (is_object($g2)) {
              $u->exitGroup($g2);
           }
}
function deactivate() {
           Loader::model('groups');
traisoon replied on at Permalink Reply
traisoon
Hi, I just updated my C5 to 5.4.1 and I try to create and put all these code/php files into their respective folder. Apparently it doesn't work.

By the way I set the new user registration method as "Registration is enabled, email must be validated."

site_events.php ---> www/config
<?php
   Events::extend('on_user_add',             /* event */
                  'ApplicationUser',         /* class */ 
                  'setupUserJoinInfo',       /* method */
                  'models/application_user.php'); 
?>


application_user.php ---> www/models
<?php 
class ApplicationUser extends Object {
  public function setupUserJoinInfo($ui)
  {  
     Loader::model('groups');
     $u = $ui->getUserObject(); 
     // add new user to groups
     $g1 = Group::getByName('unvalidated');
     if (is_object($g1)) { 
        $u->enterGroup($g1);
     }     
     // Notify admin that new user added
     $adminUserInfo=UserInfo::getByID(USER_SUPER_ID);
     $adminEmailAddress = $adminUserInfo->getUserEmail(); 
     $mh = Loader::helper('mail');


notify_new_user_added.php ---> www/mail
<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$subject = SITE . " " . t("Notification - New User Added");
$body = t("
A new user has registered on your website.
User Name: %s
Email Address: %s
", $uName, $uEmail);
foreach($attribs as $item) {
   $body .= $item . "\n";
}
$body .= t("
This account may be managed directly at
%s", BASE_URL . View::url('/dashboard/users/search?uID=' . $uID));


add new line code to site.php ---> www/config
define('EMAIL_DEFAULT_FROM_ADDRESS', 'noreply@xxxxxx.com');
define('ENABLE_APPLICATION_EVENTS',true);


Hopefully you guys can help me out.

Thanks!
traisoon replied on at Permalink Reply
traisoon
Answer to my own question. lol!

For the new code added to www/config/site.php, It should be:
define('ENABLE_APPLICATION_EVENTS',true);

then followed by
define('EMAIL_DEFAULT_FROM_ADDRESS', 'noreply@xxxxxx.com');


Thanks to all the post above. ^_^
PassionForCreative replied on at Permalink Reply
PassionForCreative
Thanks for this guys. I've copied the code from above (thanks traisoon) & I have set it up to auto-email the site moderator using the getByEmail call but I don't get an email address or username when the email comes. It's blank & looks like this:

A new user has registered on FairwayFriends.ie.
User Name:
Email Address:

This account may be managed directly at…

Can anyone tell me why?
micrdy replied on at Permalink Reply
micrdy
Any chance to set the FROM_NAME of the registration confirmed mail?
e.g.

MyName
example@example.com
jordanlev replied on at Permalink Reply
jordanlev
define('EMAIL_DEFAULT_FROM_NAME', 'My Name');
micrdy replied on at Permalink Reply
micrdy
I got this already. If the user registers and after that i activate the user account, the name of the email sent from is "Notification of user-registration" (in german).
Hoopergraphics replied on at Permalink Reply
Hoopergraphics
Hi, I have entered the coding outlined here and the administrator has received notification. The problem now is that the person registering when they hit the Register Button the following errors come up:


Warning: Invalid argument supplied for foreach() in /home/guides/public_html/mail/notify_new_user_added.php on line 9

Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in /home/guides/public_html/concrete/models/user.php on line 139

Warning: Cannot modify header information - headers already sent by (output started at /home/guides/public_html/mail/notify_new_user_added.php:9) in /home/guides/public_html/concrete/libraries/controller.php on line 350

Their actual registering details are recorded in the User and Groups page and an email is sent to the admin. But can't seem to get rid of these errors.

Some other posts have suggested changing the php.ini file but I am unable to find that.

Any sugestions would be gratefully accpeted.
cursal replied on at Permalink Reply
cursal
Did you try jordanlev solution below?

C5 has this feature built in using the code provided and placed in the config.
Hoopergraphics replied on at Permalink Reply
Hoopergraphics
Thanks for quick response, yes we have tried that and it works brilliantly. However after people complete the form it comes up with the above error message/warnings so we are trying to find what causes that.
hereNT replied on at Permalink Reply
hereNT
I think it's giving you the error because $attribs is undefined in the email template.

For people looking to send an email with the details of your new user, you need to define a different event than on_user_add. The reason for this I've written up here:

http://www.werstnet.com/blog/a-more-useful-on-user-add-event-for-co...

Basically on_user_add fires before the attributes are saved, so the $ui object that you get back has no profile information included. If you fire off a second event after the attributes are saved you have a full UserInfo object to work with, and can grab whatever information from it you need.
Hoopergraphics replied on at Permalink Reply
Hoopergraphics
Thanks for that - I will attempt to have a go at that!
sschildbach replied on at Permalink Reply
sschildbach
Thanks dg1 & traisoon,
But when I follow your directions I get php errors (using ver 5.4.1.1). Any idea why?

Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/seattlemusiciansC5/mail/notify_new_user_added.php on line 9

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/seattlemusiciansC5/mail/notify_new_user_added.php:9) in /Applications/MAMP/htdocs/seattlemusiciansC5/concrete/libraries/controller.php on line 333
jordanlev replied on at Permalink Reply
jordanlev
Just a note to future viewers: recent versions of Concrete5 have this functionality built in. Just add this to your config/site.php file:
define('EMAIL_ADDRESS_REGISTER_NOTIFICATION', 'example@domain.com');

(set example@domain.com to the email address you want the notifications sent to).

You can also optionally add this if you want the notification email to come from a specific address:
define('EMAIL_ADDRESS_REGISTER_NOTIFICATION_FROM', 'example@domain.com');

(if you don't include this, the super-admin user's email address will be used for the "from" address).
dantheman replied on at Permalink Reply
Perfect - great to see this answer at the end of the discussion!
Thanks a lot jordanlev
ralanyo replied on at Permalink Reply
Is there something built in like this to send an email to users when they are manually activated?
shasha1 replied on at Permalink Reply
relanyo, did you get a solution for an email to users when their account has been activated manually? I would be keen on knowing this.
hereNT replied on at Permalink Reply
hereNT
There currently is no way. I've patched the user edit page to fire a custom event 'on_user_approve' with a custom function that emails the user to let them know their account is active, but it's not exactly simple.
cursal replied on at Permalink Reply
cursal
This really should have the best answer added to it. The above old answer may have been best at the time.

New versions, better solutions.

Thanks jordanlev
jordanlev replied on at Permalink Reply
jordanlev
Good point -- fixed.
cursal replied on at Permalink Reply
cursal
Right on/
Works just like it should.
tfountain replied on at Permalink Reply
tfountain
To save someone else an hour of trouble...

The path here is wrong. You need to edit config/site.php and add the appropriate lines.

site/config.php didn't exist so I added it then couldn't figure out why it wouldn't work. The path/filename were transposed in the original instructions.
jordanlev replied on at Permalink Reply
jordanlev
Oops, sorry about that -- I've edited the answer to fix this.
tfountain replied on at Permalink Reply
tfountain
Well I'm a little disappointed about the response time.

joking ... 15 minutes to get that fixed is super!

Thanks!
ArchiCentre replied on at Permalink Reply
ArchiCentre
Hi,
After adding:
define('EMAIL_ADDRESS_REGISTER_NOTIFICATION', 'me@myemailaddress.com');

into config/site.php I'm getting similar errors to HooperGraphics (post Sept 7, 2011 at 10:24am) see below:

I see additional instructions at:
http://www.werstnet.com/blog/a-more-useful-on-user-add-event-for-co...

as a bit of a newbie my head is starting to hurt so I'm hoping there's a simpler work-around,
anyone?



Warning: Invalid argument supplied for foreach() in ...../updates/concrete5.5.1/concrete/mail/user_register_approval_required.php on line 14
Warning: Cannot modify header information - headers already sent by (output started at ..../updates/concrete5.5.1/concrete/mail/user_register_approval_required.php:14) in ......./updates/concrete5.5.1/concrete/models/user.php on line 74
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at ......./updates/concrete5.5.1/concrete/mail/user_register_approval_required.php:14) in ......./updates/concrete5.5.1/concrete/models/user.php on line 76
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at ........./updates/concrete5.5.1/concrete/mail/user_register_approval_required.php:14) in .........../updates/concrete5.5.1/concrete/models/user.php on line 76
Warning: Cannot modify header information - headers already sent by (output started at ................./updates/concrete5.5.1/concrete/mail/user_register_approval_required.php:14) in ............./updates/concrete5.5.1/concrete/libraries/controller.php on line 357
geetarpicker replied on at Permalink Reply
Hi all!

Seeing as this is still quite an active discussion, I won't feel bad about jumping in here... I successfully implemented the changes described to email a notification to the "Admin" regarding a new user registration event. I also was able to get an email to fire to the registrant, but there is one minor issue:
The email that gets sent to a registrant upon manual approval of the account arrives with the Username and Email "fields" in the email completely blank. The email gets addressed to the proper email address and successfully gets sent, though.

My guess is that the approval on an account is not a "POST" site event and that the template I'm using will need to be changed accordingly. The template is:

<?php
defined('C5_EXECUTE') or die("Access Denied.");
$subject = SITE . " " . t("Registration - Approved");
$body = t("
You have requested a user login for the site.
User Name: %s
Email Address: %s
", $_POST['uName'], $_POST['uEmail']);
foreach($attribs as $item) {
        $body .= $item . "\n";
}
$body .= t("
This account has been approved by the site administrator. It is active and may be used to log in.
Thank you for your registration!
");


Any help would be great because, although technically working, sending an email with blank registration information does not really portray a professional site.

Many thanks!
jordanlev replied on at Permalink Reply
jordanlev
Your theory on what's happening sounds correct to me (I assume this means that the email going to the admin *does* work, but the additional email you're sending to the registrant is not).

What is the code you're using to send the email to the registrant? (I couldn't actually find anything in this thread about that -- only code for sending to the admin)? If you can post that code then I could show you how to modify it to make this work.
Or if you want to do it yourself, basically I think what needs to happen is you need to duplicate the current email template (make a copy of it and give it a different name), then change the copy of the template to use normal variables instead of $_POST values, then the code that's loading the template needs to load the altered copy template instead and pass in those variable values.
Hoopergraphics replied on at Permalink Reply
Hoopergraphics
Did anyone else receive loads of "spam" related to this post? I said I didn't want to monitor this discussion and now they have disappeared? I had 188 emails in 18 hours.

If it was something I had done, can anyone advise what I did wrong, because obviously I don't want it happening again, or causing it to happen to other people.

Thanks
LucasAnderson replied on at Permalink Reply
LucasAnderson
Nothing you did wrong. A spammer got in and posted a lot over the last 18 hours or so. The core team has just removed all the spam messages.

Unfortunately, anyone who was monitoring would have got all the notification emails.
Hoopergraphics replied on at Permalink Reply
Hoopergraphics
Thanks Lucas, the Forums etc are invaluable to people like me.

I suppose it is the "joys" of the internet, these things happen, which is a shame but just highlights unethical "black hat" SEO companies, whilst creating a good example for me to warn my customers about!
alancurrall replied on at Permalink Reply
alancurrall
So what was the final outcome and what do I need to do to:
1: Have an e-mail notification sent to me so I know when I have to activate a users account.
2: Send an e-mail to the user notifying them that their account has been activated.

Clear easy to understand instructions: -
1: Exactly what files to create
2: Exact script
3: Exactly where to put files

Not much to ask !

Many thanks in advance,
Alan.
alancurrall replied on at Permalink Reply
alancurrall
Hi
So what was the final outcome and what do I need to do to:
1: Have an e-mail notification sent to me so I know when I have to activate a users account.
2: Send an e-mail to the user notifying them that their account has been activated.

Clear easy to understand instructions: -
1: Exactly what files to create
2: Exact script
3: Exactly where to put files

Not much to ask !

Many thanks in advance,
Alan.
dforst replied on at Permalink Reply
I'll second that request, and tip a good reply.
frz replied on at Permalink Best Answer Reply
frz
Just thought I'd point out that you can do this from within the dashboard in concrete5.5.2.1.

Just goto the Public Registration settings page by typing "registr.." into intelligent search. Change. Dig the options.
dforst replied on at Permalink Reply
Hmmmm, not sending any emails. Either to admin or to user. Do I need to define anything in config?
gte037p replied on at Permalink Reply
I'm having the same problem. No emails are being sent to either the admin or the registrant. Any ideas?
lawlerfa replied on at Permalink Reply
lawlerfa
I'm having the same problem, too. Whenever a new user registers, the notification email is not being sent. Has anyone figured out what's going on?
shotrox replied on at Permalink Reply
shotrox
I know this is a long and old thread but I used this today and run into a small issue - maybe you can help me

In the email that gets send to the user I also want the password to be sent... and it only shows up encrypted in the email.

here is my code in application_user
$mh = Loader::helper('mail');
      $mh->to($userEmailAddress);
      $mh->addParameter('userName', $ui->getUserName());
      $mh->addParameter('userEmail', $ui->getUserEmail());
      $mh->addParameter('password', $ui->getUserPassword());
     $mh->load('user_activation');
      $mh->sendMail();


what would be the better parameter to pull the password?

Thanks!
moth replied on at Permalink Reply
moth
You can't get a plain-text password via the API methods.

Passwords are encrypted for security purposes. If you can't get them out of the database, then neither can a hacker.

The only way you would be able to do this would be to send the raw post password variable before it gets encrypted.

Personally, I get worried when websites send me my password in plaintext over email. It makes me think they might not being taking security to seriously.
theneptune replied on at Permalink Reply
theneptune
Really appreciate. Thanks.