I would like to set up Postmaster or Sprout Email to send an e-mail notification when a new entry is added. I only want the email sent to users who are linked to the entry via a custom field (type:user).

I've having trouble solving this without writing a plugin to extend either plugin. If I have to write a plugin to do this, any tips? Otherwise, how can I do this with either plugin?

To reiterate:

How can I have notifications sent only to users who are linked to the new entry via a custom field (type:user)?

  • 1
    How is the "new entry" being added? Are you doing it from the back end or front end? Sep 23 '16 at 9:29

If I'm correct in thinking that you want users who have been added to a relation field on an entry to receive an email, then the following will work.

You will have to write a plugin to listen for the onSaveEntry event. You can then loop through any users added to the custom field of the entry and send them an email.

The code would look something like the following:

craft()->on( 'entries.onSaveEntry', function( Event $event ){

    $entry = $event->params['entry'];
    // The users field returns user id's
    $userIds = $entry->getContent()->users;

    // Grab the user models using the id's from the entry field
    $criteria = craft()->elements->getCriteria( ElementType::User );
    $criteria->id = $userIds;
    $users = $criteria->find();

    // If we have users
    if ( $users ){
        // For each user
        foreach ( $users as $user ){
            // Builds and sents the email
            $email = new EmailModel();
            $email->toEmail = $user->email;
            $email->subject = "You've been added to an entry";
            // Use this for a plain text email body
            $email->body = '';
            // This uses a html body from a twig template, you can pass the entry to it to access the entries fields
            $email->htmlBody = craft()->templates->render( 'pluginname/_emails/email-template-name', array(
                'entry' => $entry
            ) );
            craft()->email->sendEmail( $email );
  • This works great, thank you! And that plugin generator will be very helpful in the future.
    – tosegard
    Sep 23 '16 at 19:53

You can do this in Sprout Email by creating a Custom Event Integration. I'm not familiar with Postmaster.

While Sprout Email comes with a When an Entry is Saved event, it's not going to work for you because the Users field submits data via User IDs to create the relations and you can't create a list of recipients from the IDs along, before some extra processing.

You could create a new Custom Event which uses SproutEmail_EntriesSaveEntryEvent as a starting point. The SproutEmail_EntriesSaveEntryEvent can be found at sproutemail/integrations/sproutemail/SproutEmail_EntriesSaveEntryEvent.php

With a Custom Event you can customize what options are available to the Event, or just make bold assumptions based on the way you plan to be using the Event.

In your case, you'll want to update the prepareParams method to find your user field and process it to get the user emails you need, and then return the list of emails as an array in the event object.

public function prepareParams(Event $event)
    // Process your User Field and get your emails
    $userEmails => $this->processUsersFieldIdsIntoEmails($event->params['entry']);

    return array(
        'value' => array(
          'userEmails' => $userEmails

Your dynamic event object as specified by the value parameter becomes available to your Sprout Email using Craft's object syntax.

You could then use this value in the Recipients field of your Notification Email :

{{ object.userEmails|join(',') }}

Syntax may vary depending on how you choose to hand back your list of emails, and any other variables you may want to exist in the dynamic object to use throughout your email.

  • I ended up using the standalone plugin because it removed the need for Sprout. Great answer though, very thorough.
    – tosegard
    Sep 23 '16 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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