<p>Use <code>craft()-&gt;elements-&gt;saveElement</code> the second time. It will work exactly the same but won't trigger the onSaveEntry event.</p>

CC BY-SA 3.0
2015-06-22T11:25:34.410Z
2015-06-22T11:25:34.410Z
Bob Olde Hampsink
<p>I have written a plugin that checks if an entry has been saved and sends the user an email from within an onSaveEntry event. This works fine. But I now need to also update this entry to flag that an email has been sent so that subsequent saves do not fire the event. I have tried adding the following but these lines do not work and cause an infinite loop.</p>

<pre><code>$entry-&gt;setContentFromPost(['emailSent' =&gt; '1']);
craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre>

<p>Is there a way to re-save the entry after the email has been sent so as just to update the emailSent field?</p>

<p>Full function:</p>

<pre><code>public function onSaveEntry(Event $event)
{
    $entry = $event-&gt;params['entry'];
    if($entry-&gt;sectionId == '12345') {
        if($user = craft()-&gt;users-&gt;getUserById($entry-&gt;authorId)) {
            if($entry-&gt;enabled &amp;&amp; !$user-&gt;admin &amp;&amp; !$entry-&gt;emailSent) {

                // Send Approved Mail
                $email = new EmailModel();
                $email-&gt;fromEmail = 'me@mydomain.com';
                $email-&gt;toEmail = $user-&gt;email;
                $email-&gt;subject = 'Email Subject';
                $email-&gt;body    = '&lt;p&gt;Email Body&lt;/p&gt;';
                craft()-&gt;email-&gt;sendEmail($email);

                $entry-&gt;setContentFromPost(['emailSent' =&gt; '1']);
                craft()-&gt;entries-&gt;saveEntry($entry);
            }
        }
    }
    return $event;
}
</code></pre>

<plugin-development><entries>
Updating an entry after an event action within onSaveEntry
# Updating an entry after an event action within onSaveEntry

(Asked by: username_0 on 2015-06-22)

I have written a plugin that checks if an entry has been saved and sends the user an email from within an onSaveEntry event. This works fine. But I now need to also update this entry to flag that an email has been sent so that subsequent saves do not fire the event. I have tried adding the following but these lines do not work and cause an infinite loop.
$entry->setContentFromPost(['emailSent' => '1']);
craft()->entries->saveEntry($entry);

Is there a way to re-save the entry after the email has been sent so as just to update the emailSent field?
Full function:
public function onSaveEntry(Event $event)
{
    $entry = $event->params['entry'];
    if($entry->sectionId == '12345') {
        if($user = craft()->users->getUserById($entry->authorId)) {
            if($entry->enabled && !$user->admin && !$entry->emailSent) {

                // Send Approved Mail
                $email = new EmailModel();
                $email->fromEmail = 'me@mydomain.com';
                $email->toEmail = $user->email;
                $email->subject = 'Email Subject';
                $email->body    = 'Email Body';
                craft()->email->sendEmail($email);

                $entry->setContentFromPost(['emailSent' => '1']);
                craft()->entries->saveEntry($entry);
            }
        }
    }
    return $event;
}

**Tags:** plugin-development, entries


---

## 1 answers

### Answer by username_1 on 2015-06-22. Score: 7 | Accepted answer

Use `craft()->elements->saveElement` the second time. It will work exactly the same but won't trigger the onSaveEntry event.


---

