<p>Your code is pretty close. You want to make sure it's a new entry and not a draft or a revision so you could use:</p>
<pre class="lang-php prettyprint-override"><code>Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE, static function (ModelEvent $event) {

    /** Entry @entry */
    $entry = $event-&gt;sender;

    /** @var User $user */
    $user = Craft::$app-&gt;getUser();

    if (
        $event-&gt;isNew &amp;&amp; // Makes sure it's a new entry
        $user-&gt;identity-&gt;isInGroup('cook') &amp;&amp; // Makes sure your user is in the cook group
        !ElementHelper::isDraftOrRevision($entry) // Makes it's not a draft or a revision
    ) {  
            $cpUrl = $entry-&gt;cpEditUrl;
            $bodyText = &quot;Dear Admin. /n $userName has created &lt;a href=$cpUrl&gt;$entry&lt;/a&gt;.&quot;;
    }
});
</code></pre>

CC BY-SA 4.0
2021-01-25T15:47:39.513Z
Ok, that don't work - because the 'cook' can only save drafts - not publish.
Slowwie
CC BY-SA 4.0
2021-01-25T15:50:27.500Z
Anyway - I found a solution. When I test `$request->isPost` then it works. the initial save event is a get request to the edit site.  Maybe I have to add a conditional for not allow ajax calls and only react on the first post request.
Slowwie
CC BY-SA 4.0
2021-01-25T12:18:31.380Z
2022-07-25T12:26:51.043Z
2022-07-25T12:26:51.043Z
Oli
<p>I want to send a Mail to an admin, whenever a User of group ‚Cook‘ creates an Entry of Type ‚Recipe‘.</p>
<p>For that I created an Event reacting on Entry::EVENT_AFTER_SAVE :</p>
<pre><code>Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE, static function (ModelEvent $event) {

    /** Entry @entry */
    $entry = $event-&gt;sender;

    /** @var User $user */
    $user = Craft::$app-&gt;getUser();

    if (!$event-&gt;isNew) {
        if ($user-&gt;identity-&gt;isInGroup('cook') &amp;&amp;
            ElementHelper::isDraftOrRevision($entry) &amp;&amp;
            $entry-&gt;getStatus() === Entry::STATUS_DISABLED) {

         
            $cpUrl = $entry-&gt;cpEditUrl;
            $bodyText = &quot;Dear Admin. /n $userName has created &lt;a href=$cpUrl&gt;$entry&lt;/a&gt;.&quot;;
        

        };
    }
});
</code></pre>
<p>As your can see - I tried a lot to only react on the first creation of the entry. But all this does not the trick.
Behind the scenes craft created an Entry (and saves it initially) even when the Cook presses the ’New Entry’ Button.</p>
<p>So I don’t know hook in only when the message is first saved directly by the user pressing the ‚Create‘ Button after entering the content.</p>
<p>It would be nice if your could help me with that.</p>
<p>Thanks in advance.
Michael</p>

CC BY-SA 4.0
2021-01-25T02:19:29.950Z
You only want this to fire on new entries?
RitterKnight
CC BY-SA 4.0
2021-01-25T03:37:02.553Z
Yes - the admin just wants to have one email, when the user saved a new recipe first time.
Slowwie
<craft3><events><element>
Hook into Entry::EVENT_AFTER_SAVE when user saves it
# Hook into Entry::EVENT_AFTER_SAVE when user saves it

(Asked by: username_0 on 2021-01-24)

I want to send a Mail to an admin, whenever a User of group ‚Cook‘ creates an Entry of Type ‚Recipe‘.
For that I created an Event reacting on Entry::EVENT_AFTER_SAVE :
Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE, static function (ModelEvent $event) {

    /** Entry @entry */
    $entry = $event->sender;

    /** @var User $user */
    $user = Craft::$app->getUser();

    if (!$event->isNew) {
        if ($user->identity->isInGroup('cook') &&
            ElementHelper::isDraftOrRevision($entry) &&
            $entry->getStatus() === Entry::STATUS_DISABLED) {

         
            $cpUrl = $entry->cpEditUrl;
            $bodyText = "Dear Admin. /n $userName has created $entry.";
        

        };
    }
});

As your can see - I tried a lot to only react on the first creation of the entry. But all this does not the trick.
Behind the scenes craft created an Entry (and saves it initially) even when the Cook presses the ’New Entry’ Button.
So I don’t know hook in only when the message is first saved directly by the user pressing the ‚Create‘ Button after entering the content.
It would be nice if your could help me with that.
Thanks in advance.
Michael

**Tags:** craft3, events, element
> **Comments:**
> **username_1**: You only want this to fire on new entries?  
> **username_0**: Yes - the admin just wants to have one email, when the user saved a new recipe first time.  

---

## 1 answers

### Answer by username_2 on 2021-01-25. Score: 3

Your code is pretty close. You want to make sure it's a new entry and not a draft or a revision so you could use:
Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE, static function (ModelEvent $event) {

    /** Entry @entry */
    $entry = $event->sender;

    /** @var User $user */
    $user = Craft::$app->getUser();

    if (
        $event->isNew && // Makes sure it's a new entry
        $user->identity->isInGroup('cook') && // Makes sure your user is in the cook group
        !ElementHelper::isDraftOrRevision($entry) // Makes it's not a draft or a revision
    ) {  
            $cpUrl = $entry->cpEditUrl;
            $bodyText = "Dear Admin. /n $userName has created $entry.";
    }
});

> **Comments:**
> **username_0**: Ok, that don't work - because the 'cook' can only save drafts - not publish.  
> **username_0**: Anyway - I found a solution. When I test `$request->isPost` then it works. the initial save event is a get request to the edit site.  Maybe I have to add a conditional for not allow ajax calls and only react on the first post request.  

---

