2

I have a client that wants to be notified by email whenever any user on the site updates their profile fields. Right now, users can update their profiles through a front end form.

Ideally, after any update, an email will be sent to the content admin of the site (not main admin user but another user group). I assume that a plugin will be needed but I have zero clue where and how to start. Any help would be highly appreciated.

2
  • Do you also want to send the notification if an admin updates someone's profile from the Craft control panel? Commented Feb 11, 2022 at 15:31
  • I am trying to only send the notification when the user is being updated via the front end form Commented Feb 11, 2022 at 17:56

1 Answer 1

2

Here's a simplified version of how I'm doing that in a custom module.

You can create a Twig template at emails/customer-details-updated and it'll receive a user variable from this code with all the user's fields. At the time I couldn't find an easy way to find exactly which fields had been updated other than querying for the user data and looping through every field to compare the post data, which seemed like a lot of effort... but let me know if you manage to do that!


<?php

use craft\services\Elements;
use craft\mail\Message;
use craft\web\View;
use yii\base\Event;
use yii\base\Module;

class Site extends Module
{
    public function init()
    {
        // --------------------------------------------
        // NOTIFY ADMIN WHEN A USER UPDATES THEIR DETAILS
        // or if they are updated in the CP.
        // --------------------------------------------

        Event::on(
            Elements::class,
            Elements::EVENT_BEFORE_SAVE_ELEMENT,
            function (Event $e) {
                
                // bail out if it's not a User being saved
                if (!$e->element instanceof \craft\elements\User) { return; }

                // set variables for consumption by the template
                $templateVars = ['user' => $e->element];

                // if it's a CP request, we'll need to switch the template mode to 'TEMPLATE_MODE_SITE'
                // in order to access the site templates for rendering the email body
                $oldMode = Craft::$app->view->getTemplateMode();
                Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE);

                // build the email body using a site template now that we're in the right template mode...
                $htmlBody = Craft::$app->view->renderTemplate('emails/customer-details-updated', $templateVars);

                // ...then put it back again.
                Craft::$app->view->setTemplateMode($oldMode);

                // assemble and send...
                $message = (new Message())
                    ->setSubject('Alert: A customer updated their profile')
                    ->setHtmlBody($htmlBody)
                    ->setTo('[email protected]');

                Craft::$app->getMailer()->send($message);
            }
        );
    }
}

Edit to add: if you want to ensure that the email only triggers from front-end edits, and not from edits made in the Craft control panel, add an early return statement based on the request's isCpRequest property (Craft::$app->request->isCpRequest) e.g.

if (Craft::$app->request->isCpRequest) { return; }
4
  • ^ Great answer! Minor simplification: since Craft 3.4.0 it's not necessary anymore to switch the template mode around with setTemplateMode() and getTemplateMode(). Instead, you can simply pass the desired template mode as the third parameter to the renderTemplate() method: Craft::$app->view->renderTemplate('emails/customer-details-updated', $templateVars, View::TEMPLATE_MODE_SITE);. Commented Feb 11, 2022 at 16:14
  • oooh cool thanks, that always seemed like a messy inconvenience. Commented Feb 11, 2022 at 17:25
  • Thank you James. Sorry I have never done something like that. What should I do with this code? Create a module.php file? I tried to use the documentations on craft but it doesn't seem to work... Commented Feb 11, 2022 at 17:56
  • Instructions for creating/configuring a custom module are listed here: craftcms.com/docs/3.x/extend/… - can you be more specific about what doesn't work? Commented Feb 14, 2022 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.