I have a custom plugin that should fire off emails based on a saved search function. The function seems to work when I enter one specific email but not when looping through profiles.

Here is the code:

function saved_search_send_event_hook($timeslot = 'onceaday') {
    $blogname = get_bloginfo();
    global $wpdb;
    $users = $wpdb->get_results( "SELECT * FROM $wpdb->users AS u
        LEFT JOIN wp_m_membership_relationships AS mmr ON mmr.user_id = u.id
        LEFT JOIN wp_m_subscriptions AS s ON mmr.sub_id = s.id
        WHERE s.sub_name = 'AnActiveWithMembership'" );
    foreach($users as $user) {
        $profiles = erns_saved_search_getprofiles($user->ID, $timeslot);
        if(!empty($profiles['count'])) {
            // The code works with 'john@example.com' on this line instead of $user->email
            $to = $user->email; 
            $subject = '[' . $blogname . '] ' . trim('Saved Searches');

I feel I am overlooking something here. Please let me know if you need any other information. Wordpress v4.1

  • I don't see where you're sending email in that code. – Milo Feb 23 '15 at 1:47
  • Thanks for the question @Milo. What code are you expecting? Its sending to wp_mail function. As stated in the code description, I receive emails when I enter a direct email address instead of $to = $user->email. I am thinking there is a problem with the loop that is not getting the email address. – user3795286 Feb 23 '15 at 5:56

The user's email column is user_email. Always develop with debugging enabled to catch errors in your code.

|improve this answer|||||
  • Thank you for your response. I tried with $user->user_email and it still did not work. I do have debugging enabled and when I use $user->user_email the debug warning goes away but there emails do not fire. Any other suggestion? – user3795286 Feb 23 '15 at 18:14
  • like I said in my other comment, there's nothing in the code you've shown that actually sends the email, so it's difficult to say where the issue may be. If that's not the complete code, you should edit it to show everything. if that is the complete code, I don't see how it could possibly work in any case. – Milo Feb 23 '15 at 18:26
  • The issue was that there were invalid email addresses in my test accounts and the emails were not firing because of that. However the $user-email was a problem and the $user->user_email was the solution so I am marking your solution as correct. Thank you – user3795286 Feb 25 '15 at 22:07

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.