<p>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.</p>

<p>You will have to write a <a href="https://pluginfactory.io/" rel="nofollow">plugin</a> to listen for the <a href="https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry" rel="nofollow">onSaveEntry</a> event. You can then loop through any users added to the custom field of the entry and send them an email.</p>

<p>The code would look something like the following:</p>

<pre><code>craft()-&gt;on( 'entries.onSaveEntry', function( Event $event ){

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

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

    // If we have users
    if ( $users ){
        // For each user
        foreach ( $users as $user ){
            // Builds and sents the email
            $email = new EmailModel();
            $email-&gt;toEmail = $user-&gt;email;
            $email-&gt;subject = "You've been added to an entry";
            // Use this for a plain text email body
            $email-&gt;body = '';
            // This uses a html body from a twig template, you can pass the entry to it to access the entries fields
            $email-&gt;htmlBody = craft()-&gt;templates-&gt;render( 'pluginname/_emails/email-template-name', array(
                'entry' =&gt; $entry
            ) );
            craft()-&gt;email-&gt;sendEmail( $email );
        }
    }
});
</code></pre>

CC BY-SA 3.0
2016-09-23T19:53:34.217Z
This works great, thank you! And that plugin generator will be very helpful in the future.
tosegard
CC BY-SA 3.0
2016-09-23T09:48:28.753Z
2016-09-23T09:48:28.753Z
Luke Pearce
<p>You can do this in Sprout Email by creating a <a href="http://sprout.barrelstrengthdesign.com/craft-plugins/email/docs/notifications/custom-events" rel="nofollow">Custom Event Integration</a>. I'm not familiar with Postmaster.</p>

<p>While Sprout Email comes with a <em>When an Entry is Saved</em> 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.</p>

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

<p>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.</p>

<p>In your case, you'll want to update the <code>prepareParams</code> 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.</p>

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

    return array(
        'value' =&gt; array(
          'userEmails' =&gt; $userEmails
        ),
    );
}
</code></pre>

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

<p>You could then use this value in the Recipients field of your Notification Email :</p>

<pre><code>{{ object.userEmails|join(',') }}
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2016-09-23T19:55:45.813Z
I ended up using the standalone plugin because it removed the need for Sprout. Great answer though, very thorough.
tosegard
CC BY-SA 3.0
2016-09-23T15:54:04.377Z
2016-09-23T15:54:04.377Z
Ben Parizek
<p>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).</p>

<p>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?</p>

<p>To reiterate:</p>

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

CC BY-SA 3.0
2016-09-23T09:29:39.220Z
How is the "new entry" being added? Are you doing it from the back end or front end?
Luke Pearce
<plugins><emails><plugin-sproutemail>
Send e-mail only to users related to entry with Postmaster/Sprout
# Send e-mail only to users related to entry with Postmaster/Sprout

(Asked by: username_0 on 2016-09-23)

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)?**

**Tags:** plugins, emails, plugin-sproutemail
> **Comments:**
> **username_1**: How is the "new entry" being added? Are you doing it from the back end or front end?  

---

## 2 answers

### Answer by username_1 on 2016-09-23. Score: 1 | Accepted answer

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 );
        }
    }
});

> **Comments:**
> **username_0**: This works great, thank you! And that plugin generator will be very helpful in the future.  

---

### Answer by username_2 on 2016-09-23. Score: 1

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.

> **Comments:**
> **username_0**: I ended up using the standalone plugin because it removed the need for Sprout. Great answer though, very thorough.  

---

