<p>Is there some difference between admin users and other users? In my plugin, I have code that grabs the email address off of a selected user and then sends an email to that user. It works fine is the user is not an admin, but breaks if the user is an admin.</p>

<pre><code>// grab the user object so we can get the email address from it
foreach ($users as $user) {
    if ($user-&gt;id == $settings-&gt;users) {
        $emailRecipient = $user;
    }
}
</code></pre>

<p>Later, I pull the email address and send the email.</p>

<pre><code>$email          = new EmailModel();
$email-&gt;toEmail = $emailRecipient-&gt;email;
$email-&gt;subject = 'New Entry in ' . $sectionTitle . '.';
$email-&gt;body    = $body;
</code></pre>

<p>For non admins, it works but for admins, I get the error <code>PHP notice: Trying to get property of non-object</code></p>

<p>Any ideas?</p>

CC BY-SA 3.0
2016-01-05T23:16:55.833Z
What's `$settings->users`? That sounds like an array whereas `$user->id` is singular, so `$user->id == $settings->users` doesn't make sense.
Brad Bell
CC BY-SA 3.0
2016-01-06T03:20:28.663Z
That's a naming convention relic. Should just be $settings->user or $settings->selectedUser. I have all users being pulled into a dropdown in the plugin settings, so you can select which user you want the email to go to. The email sends and everything works fine if you don't choose the admin user.
idahotallpaul
CC BY-SA 3.0
2016-01-06T19:20:24.403Z
I suspect there is something else going on elsewhere in your code causing the behavior, but for whatever reason `$emailRecipient` isn't a UserModel object when you get that error.  You can try `var_dump`ing it to get some insight.
Brad Bell
<plugin-development><users><php><admin>
Problem pulling email address from admin user in plugin
# Problem pulling email address from admin user in plugin

(Asked by: username_0 on 2016-01-05)

Is there some difference between admin users and other users? In my plugin, I have code that grabs the email address off of a selected user and then sends an email to that user. It works fine is the user is not an admin, but breaks if the user is an admin.
// grab the user object so we can get the email address from it
foreach ($users as $user) {
    if ($user->id == $settings->users) {
        $emailRecipient = $user;
    }
}

Later, I pull the email address and send the email.
$email          = new EmailModel();
$email->toEmail = $emailRecipient->email;
$email->subject = 'New Entry in ' . $sectionTitle . '.';
$email->body    = $body;

For non admins, it works but for admins, I get the error PHP notice: Trying to get property of non-object
Any ideas?

**Tags:** plugin-development, users, php, admin

> **Comments:**
> **username_1**: What's `$settings->users`? That sounds like an array whereas `$user->id` is singular, so `$user->id == $settings->users` doesn't make sense.  
> **username_0**: That's a naming convention relic. Should just be $settings->user or $settings->selectedUser. I have all users being pulled into a dropdown in the plugin settings, so you can select which user you want the email to go to. The email sends and everything works fine if you don't choose the admin user.  
> **username_1**: I suspect there is something else going on elsewhere in your code causing the behavior, but for whatever reason `$emailRecipient` isn't a UserModel object when you get that error.  You can try `var_dump`ing it to get some insight.  

---

## 0 answers

