0

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?

  • 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 Jan 5 '16 at 23:16
  • 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 Jan 6 '16 at 3:20
  • 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_dumping it to get some insight. – Brad Bell Jan 6 '16 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.