<p>You should be able to do something like this using the event:</p>

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

  $testfield = craft()-&gt;request-&gt;getPost('someField');

  if($testfield)
  {
    // Send the email
  }

});
</code></pre>

CC BY-SA 3.0
2016-07-28T09:55:12.123Z
I tried this, but could not pull any values into $testfield, it never got any content no matter what field I tried to pull.
Matt Wilcox
CC BY-SA 3.0
2016-07-28T09:56:27.030Z
This might be obvious, but are you sure your form has `method="POST"` or if its a `GET` request you should be able to remove the `Post` part of the function to get the value
Alec Ritson
CC BY-SA 3.0
2016-07-28T10:16:03.657Z
The `onSaveUser` event happens after the user is saved. So you don't need to get the field value from the post request - you'd just use the `user` param to check the fields value.
Luke Pearce
CC BY-SA 3.0
2016-07-28T10:17:06.450Z
True, unless its a field that doesn't get saved to the user and is purely used for the check
Alec Ritson
CC BY-SA 3.0
2016-07-28T10:20:11.847Z
Luke; you're right, except that the value will be the same for any time the user is saved again - meaning notifications sent each time. I need to test for the value of that variable - which can be done that way - but also that it's from *that form*, which i did by having a hidden input on that form.
Matt Wilcox
CC BY-SA 3.0
2016-07-28T09:52:30.890Z
2016-07-28T09:52:30.890Z
Alec Ritson
<p>I think the following will complete all your requirements. If I've understood correctly you're trying to send an email notification by using a hidden field in a front end form. You don't want the email notification to send twice and you don't want it to send if a user is new or being saved from the control panel.</p>

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

    $user = $event-&gt;params['user'];

    // Returns if the user is a new user
    if ( $event-&gt;params['isNewUser'] )
    {
      return
    }

    // If the user hasn't had an email before
    if ( $user-&gt;emailHasBeenSent == 0 )
    {
      // Get the frontEnd field value from the post request
      $frontEnd = craft()-&gt;request-&gt;getPost('frontEnd');

      // Check the frontEnd value (will be null if user saved through the CP)
      if ( $frontEnd == 1 )
      {
        // Send the email
        // Save the emailHasBeenSent on the user to true so we don't send another email the next time the user is saved
      }
    {

 });
</code></pre>

CC BY-SA 3.0
2016-07-28T13:52:18.620Z
$frontEnd = craft()->request->getPost('frontEnd'); won't work though, for the same reason the original ones don't (getPost doesn't seem to work inside the event).
Matt Wilcox
CC BY-SA 3.0
2016-07-28T14:06:54.650Z
Strange, it worked for me. At the top of the event try the line `Craft::dd( craft()->request->getPost() );` and see what it returns
Luke Pearce
CC BY-SA 3.0
2016-07-28T15:12:09.383Z
Oh my [redacted]. So, I didn't know about Craft::dd. Very useful, thanks. Turns out... it wasn't working for me because of this line: $consentGiven = craft()->request->getPost('fields[consentGiven]', 'default'); I'd been using fields[consentGive] which doesn't work. But fields.consentGiven does.
Matt Wilcox
CC BY-SA 3.0
2016-07-28T10:40:27.897Z
2016-07-28T10:58:50.307Z
2016-07-28T10:58:50.307Z
Luke Pearce
<p>I have a simple task: one particular front-end form on a website is being used to update a logged in user's profile. It works. It uses the <code>users.onSaveUser</code> action.</p>

<p>I need to fire off notification emails when a particular input value is true on form submission.</p>

<p>My first attempt was a plugin that just watched the <code>onSaveUser</code> event; but while I could get that to send email, I could not seem to get access to the form fields in order to check whether the email should be sent or not. Therefore it sent emails whenever a user was saved, including when edited in the back-end.</p>

<p>Now I'm trying a plugin that has its own action - <code>myPlugin/saveUser</code>. But I am essentially duplicating the code in the existing users/saveUser function... and that makes it brittle. Is there not a way to get this done by just testing a field property and then passing everything over to the built-in users/saveUser action?</p>

<p>How would you do this?</p>

<p>Here's the simple plugin I had, which couldn't get the POST data and was therefor no good...</p>

<pre><code>public function init() {
    craft()-&gt;on('users.saveUser', function(Event $event) {
        $this-&gt;requirePostRequest();

        // is in the relevant user group?
        if($event-&gt;params['user']-&gt;isInGroup('applications')){

            // check this is a submission from the front end form
            $applicationFormFrontend = craft()-&gt;request-&gt;getPost('applicationFormFrontend', 'default');

            // check the Consent field was true on this submission
            $consentGiven = craft()-&gt;request-&gt;getPost('fields[consentGiven]', 'default');

            NotifyPlugin::log("The value of applicationFormFrontend is $applicationFormFrontend.", LogLevel::Info);
            NotifyPlugin::log("The value of consentGiven is $consentGiven.", LogLevel::Info);

            if($applicationFormFrontend == '1' and $consentGiven == '1') {
                // Write Email

                ...

                // send the mail
                craft()-&gt;email-&gt;sendEmail($notificationEmail);
            }
        }
    });
}
</code></pre>

<p>EDIT, SOLUTION:</p>

<p>The reason I was not getting values for</p>

<pre><code>$consentGiven = craft()-&gt;request-&gt;getPost('fields[consentGiven]', 'default');
</code></pre>

<p>is because it's the wrong style. It's why it was always getting 'default'. The fix is to use dot notation:</p>

<pre><code>$consentGiven = craft()-&gt;request-&gt;getPost('fields.consentGiven', 'default');
</code></pre>

<p>The original code (as above) works with this modification.</p>

CC BY-SA 3.0
2016-07-28T10:13:15.500Z
So you'd like to send an email only if the user is being **updated** from a **front-end** form? You don't want to send an email if it's a new user or if it's being updated from the control panel?
Luke Pearce
CC BY-SA 3.0
2016-07-28T10:22:14.027Z
Also, are you able to post some/all of your form code to see if there is anything that could be causing the issue.
Alec Ritson
CC BY-SA 3.0
2016-07-28T10:27:03.360Z
Luke: spot on.

I'll edit the post to include what I'd got...
Matt Wilcox
CC BY-SA 3.0
2016-07-28T10:33:19.223Z
Remove `$this->requirePostRequest();` that's only used in controller actions. This event gets triggered by a user save and won't know if that came from the CP or the front end.
Luke Pearce
CC BY-SA 3.0
2016-07-28T10:34:36.783Z
Luke: It doesn't make any difference whether it's there or not, I added it later trying to figure out why I wasn't getting any values for the getPost call. Well, they were always 'default', which is the fallback.
Matt Wilcox
<plugin-development>
Conditional email notification on saveUser action?
# Conditional email notification on saveUser action?

(Asked by: username_0 on 2016-07-28)

I have a simple task: one particular front-end form on a website is being used to update a logged in user's profile. It works. It uses the users.onSaveUser action.
I need to fire off notification emails when a particular input value is true on form submission.
My first attempt was a plugin that just watched the onSaveUser event; but while I could get that to send email, I could not seem to get access to the form fields in order to check whether the email should be sent or not. Therefore it sent emails whenever a user was saved, including when edited in the back-end.
Now I'm trying a plugin that has its own action - myPlugin/saveUser. But I am essentially duplicating the code in the existing users/saveUser function... and that makes it brittle. Is there not a way to get this done by just testing a field property and then passing everything over to the built-in users/saveUser action?
How would you do this?
Here's the simple plugin I had, which couldn't get the POST data and was therefor no good...
public function init() {
    craft()->on('users.saveUser', function(Event $event) {
        $this->requirePostRequest();

        // is in the relevant user group?
        if($event->params['user']->isInGroup('applications')){

            // check this is a submission from the front end form
            $applicationFormFrontend = craft()->request->getPost('applicationFormFrontend', 'default');

            // check the Consent field was true on this submission
            $consentGiven = craft()->request->getPost('fields[consentGiven]', 'default');

            NotifyPlugin::log("The value of applicationFormFrontend is $applicationFormFrontend.", LogLevel::Info);
            NotifyPlugin::log("The value of consentGiven is $consentGiven.", LogLevel::Info);

            if($applicationFormFrontend == '1' and $consentGiven == '1') {
                // Write Email

                ...

                // send the mail
                craft()->email->sendEmail($notificationEmail);
            }
        }
    });
}

EDIT, SOLUTION:
The reason I was not getting values for
$consentGiven = craft()->request->getPost('fields[consentGiven]', 'default');

is because it's the wrong style. It's why it was always getting 'default'. The fix is to use dot notation:
$consentGiven = craft()->request->getPost('fields.consentGiven', 'default');

The original code (as above) works with this modification.

**Tags:** plugin-development

> **Comments:**
> **username_1**: So you'd like to send an email only if the user is being **updated** from a **front-end** form? You don't want to send an email if it's a new user or if it's being updated from the control panel?  
> **username_2**: Also, are you able to post some/all of your form code to see if there is anything that could be causing the issue.  
> **username_0**: Luke: spot on.  I'll edit the post to include what I'd got...  
> **username_1**: Remove `$this->requirePostRequest();` that's only used in controller actions. This event gets triggered by a user save and won't know if that came from the CP or the front end.  
> **username_0**: Luke: It doesn't make any difference whether it's there or not, I added it later trying to figure out why I wasn't getting any values for the getPost call. Well, they were always 'default', which is the fallback.  

---

## 2 answers

### Answer by username_2 on 2016-07-28. Score: 2

You should be able to do something like this using the event:
craft()->on('users.onSaveUser', function(Event $event) {

  $testfield = craft()->request->getPost('someField');

  if($testfield)
  {
    // Send the email
  }

});

> **Comments:**
> **username_0**: I tried this, but could not pull any values into $testfield, it never got any content no matter what field I tried to pull.  
> **username_2**: This might be obvious, but are you sure your form has `method="POST"` or if its a `GET` request you should be able to remove the `Post` part of the function to get the value  
> **username_1**: The `onSaveUser` event happens after the user is saved. So you don't need to get the field value from the post request - you'd just use the `user` param to check the fields value.  
> **username_2**: True, unless its a field that doesn't get saved to the user and is purely used for the check  
> **username_0**: Luke; you're right, except that the value will be the same for any time the user is saved again - meaning notifications sent each time. I need to test for the value of that variable - which can be done that way - but also that it's from *that form*, which i did by having a hidden input on that form.  

---

### Answer by username_1 on 2016-07-28. Score: 2

I think the following will complete all your requirements. If I've understood correctly you're trying to send an email notification by using a hidden field in a front end form. You don't want the email notification to send twice and you don't want it to send if a user is new or being saved from the control panel.
craft()->on('users.onSaveUser', function(Event $event) {

    $user = $event->params['user'];

    // Returns if the user is a new user
    if ( $event->params['isNewUser'] )
    {
      return
    }

    // If the user hasn't had an email before
    if ( $user->emailHasBeenSent == 0 )
    {
      // Get the frontEnd field value from the post request
      $frontEnd = craft()->request->getPost('frontEnd');

      // Check the frontEnd value (will be null if user saved through the CP)
      if ( $frontEnd == 1 )
      {
        // Send the email
        // Save the emailHasBeenSent on the user to true so we don't send another email the next time the user is saved
      }
    {

 });

> **Comments:**
> **username_0**: $frontEnd = craft()->request->getPost('frontEnd'); won't work though, for the same reason the original ones don't (getPost doesn't seem to work inside the event).  
> **username_1**: Strange, it worked for me. At the top of the event try the line `Craft::dd( craft()->request->getPost() );` and see what it returns  
> **username_0**: Oh my [redacted]. So, I didn't know about Craft::dd. Very useful, thanks. Turns out... it wasn't working for me because of this line: $consentGiven = craft()->request->getPost('fields[consentGiven]', 'default'); I'd been using fields[consentGive] which doesn't work. But fields.consentGiven does.  

---

