[Membership 2 Pro] Getting members email addresses

I am trying to add custom code in my plugin that gets all email addresses from members with paid membership and send them a newsletter. I was able to get active members with this code:

if( !class_exists( 'MS_Model_Membership' ) )
return;

$args = array(
'subscription_status' => 'active'
);
$members = MS_Model_Member::get_members( $args );
This returns some strange array with tons of information. However, what I need is emails from members that have paid membership only. Could you help me with that?

  • Adam Czajczyk

    Hello dave,

    I hope you're fine today!

    You could use this function, I think:

    function ms_all_members_data()
    {
        $ms_member_data = array();
    
        $members = MS_Model_Member::get_members();
        foreach( $members as $member )
        {
    
    		if( count( $member->subscriptions ) > 0 ) {
    
    			$tmp_member = array();
    
    			$tmp_member['ID'] = $member->id;
    			$tmp_member['username'] = $member->username;
    
    			$tmp_usr = get_userdata( $tmp_member['ID'] );
    
    			$tmp_member['email'] = $tmp_usr->user_email;
    
    			array_push( $ms_member_data, $tmp_member );
    
    		}
    
        }
    
        return $ms_member_data;
    }

    If you call it for example like this

    $members_info = ms_all_members_data();

    the $members_info will get a simple array consisting of a list of records of ID, username and email for each user:

    Please note: such an operation, especially if there's a lot of members, can be quite "resource costly" so I'd suggest not calling it too often/in real time each time it's needed but rather caching its results.

    I hope that helps :slight_smile:

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.