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 = '<p>Email Body</p>';
craft()->email->sendEmail($email);
$entry->setContentFromPost(['emailSent' => '1']);
craft()->entries->saveEntry($entry);
}
}
}
return $event;
}