TransWikia.com

Send email notification to user's email when their entry has been enabled

Craft CMS Asked by Noah Yamen on December 6, 2020

Running craft 2 on a customer’s site. They have users that sign up on the site to receive personalized quotes. What I’d like to do is send an email notification to that user after an admin has enabled the entry that the user created.

I can’t find any plugins that do this specifically. I’ve been trying to customize sprout forms/email to do this, but I’m not having any luck.

One Answer

You could build a small plugin to hook into Craft's onBeforeSaveEntry event (docs).

Before the entry is saved (and enabled), you'd check if it was previously disabled and its new status is live, if it is, send an email.

The code below should get you going.

Save it as NotifyPlugin.php in a craft/plugins/notify folder.

<?php
namespace Craft;

class NotifyPlugin extends BasePlugin
{
    function getName()
    {
        return Craft::t('Notify');
    }

    function getVersion()
    {
        return '1.0';
    }

    function getDeveloper()
    {
        return 'You';
    }

    function getDeveloperUrl()
    {
        return 'http://example.com';
    }

    function init()
    {
        // Trigger only from the Control Panel and Admin users
        if (craft()->request->isCpRequest() && craft()->userSession->isAdmin()) {

            // Catch the onBeforeSaveEntry event
            // https://craftcms.com/docs/2.x/plugins/events-reference.html#entries-onbeforesaveentry
            craft()->on('entries.onBeforeSaveEntry', function(Event $event) {

                // Isolate the entry from the event
                $entry = $event->params['entry'];

                // The ID of the section currently being saved
                $sectionId = $entry->sectionId;

                // The ID of the section you want to watch (change that to match your section ID)
                $sectionIdToWatch = 2;

                // Is this a new entry?
                $isNewEntry = $event->params['isNewEntry'];

                // Is it an entry from the section we are watching and NOT a new entry
                if ($sectionId == $sectionIdToWatch && !$isNewEntry) {

                    // Fetch the entry before save to use its status for comparison
                    // https://craftcms.com/docs/2.x/plugins/working-with-elements.html#fetching-elements
                    $currentEntry = craft()
                        ->elements
                        ->getCriteria(ElementType::Entry)
                        ->sectionId($sectionId)
                        ->status(null)
                        ->id($entry->id)
                        ->first();

                    // Entry status before saving
                    $currentEntryStatus = $currentEntry->status;

                    // New status being saved (from the event)
                    $statusBeingSaved = $entry->status;

                    // Are we going from disabled to live?
                    if(
                        $currentEntryStatus == BaseElementModel::DISABLED &&
                        $statusBeingSaved == EntryModel::LIVE
                    ) {
                        // Sets $toEmail to the entry's author but change to suit your needs
                        $toEmail = $entry->author->email;

                        // Prepare the message
                        $message = new EmailModel();

                        $message->subject = 'Your notification';
                        $message->body = 'Email content';
                        $message->htmlBody = '<html><body>Email content</body></html>';
                        $message->toEmail = $toEmail;

                        // Send the message
                        craft()->email->sendEmail($message);
                    }
                }
            });
        }
    }
}

Change $sectionIdToWatch to match your section ID and as is, this will send the email to the entry's author.

As I said, this is just a base to get you going but it should work :)

Correct answer by Oli on December 6, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP