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']);

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    = '<p>Email Body</p>';

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

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

|improve this answer|||||

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.