4

I have a client that wants to get notified whenever someone registers on their website.

How can I do this, is there some kind of plugin? If not how would I go about doing this?

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Branko
  • 497
  • 3
  • 12

2 Answers2

4

I have done something very similar. You'll want to create a plugin for this.

In my case, I am validating email addresses so I was checking against the onBeforeActivateUser event. If you are not validating email addresses, @Lindsey D' answer is where you want to go.

Either way, they will look about like this: I created a plugin called accounts.

MyPlugin.php

public function init()
{
    /**
     * Event listener for activating a user.
     *
     * @param Event $event
     */
    craft()->on('users.onBeforeActivateUser', function (Event $event) {
        $user = $event->params['user'];

        craft()->accounts->onSendNotificationEmail($user);
    });
}

MyService.php

/**
 * Sends a notification email.
 *
 * @param $user
 * @return bool
 */
public function onSendNotificationEmail($user)
{
    $recipient = $user->email; // get the email address passed in

    $email = new EmailModel();

    $message = '<!DOCTYPE html><html><title>My Title</title><head>';
    $message .= '<body>';
    $message .= '<table width="800" border="0" cellpadding="0" cellspacing="0">';
    $message .= '<tr><td style="text-align: center;">LOGO/BRANDING </td></tr>';
    $message .= '</table>';
    $message .= '<p>Hello,<br>There has been a new account created on the website.</p>';
    $message .= '<p>...your message here...</p>';
    $message .= '</body></html>';

    $email->subject = 'New Account Created';
    $email->body = $message;

    try {
        $email->toEmail = $recipient;

        craft()->email->sendEmail($email);
    } catch (\Exception $e) {
        return false;
    }
}

I'm sure there is a cleaner way to get your email body into the CP itself and allow the admin to change the message easily, but this works quick/well.

Damon
  • 4,706
  • 1
  • 20
  • 36
2

I don't think a plugin is available to do this specifically but you could write your own plugin and listen to the onAssignUserToDefaultGroup event and send an e-mail from there in your plugin.

Sending an e-mail can be done through the EmailService

Rias
  • 171
  • 5
  • 1
    Great answer, although I think there may be a better event for this scenario. I'd personally use the onSaveUser event, and check against isNewUser before sending the email. – Lindsey D Jun 09 '17 at 17:29