S2 Email - Simple code

Hi,

WIll this plugin work on regular/free s2member plugin? I’m only wanting a simple email notification once an admin changes a user role. I was provided a code before but it didnt work.

i did some research, i saw this code but havent tried it yet.

function kristin_send_email( $user_id, $new_role ) { $user_info = get_userdata( $user_id ); $to = $user_info->user_email; $subject = 'Role changed'; $message = "Hello " .$user_info->display_name . " your role has changed, now you are " . $role; wp_mail( $to, $subject, $message); } add_action( 'set_user_role', 'kristin_send_email' );

Thanks!

I’m not sure how S2 Email works, but it looks as if you are sending an email the Wordpress way by using wp_mail.
Before you can send an email this way you need to define the email headers first:
$headers[] = 'MIME-Version: 1.0'; $headers[] = 'Content-type: text/html; charset=UTF-8'; $headers[] = 'From: Me Myself <me@example.net>'; $headers[] = 'Cc: John Q Codex <jqc@wordpress.org>'; $headers[] = 'Cc: iluvwp@wordpress.org';
Then enter the to and subject lines followed by the message:
$to = $emailadress; $subject = 'Nice email'; $message = 'Hello';
I prefer to know if everything goes wrong or not so I send the email like this:
$send_mail = wp_mail($to,$subject,$message,$headers);
This way I can check if $send_mail was succesfull or not (it returns true or false):
if($send_mail) { echo 'Email sent!'; } else { echo 'Email not sent'; }
Hope this helps.

Hi, actually the code i posted ABOVE works but not perfect. it does send an email notification when the admin changes the role of a user but it also send the same email if a new user registers.

a while back Tim Khaye did send me some codes but it doesnt seem to work, maybe we’re missing a comma or a character inside it?

function kts_user_role_approve_email( $user_id, $old_user_data ) {
$site_url = site_url();

// get old user info
$old_roles = $old_user_data->roles;
 	
// get new user info
$user_info = get_userdata( $user_id );
$new_roles = $user_info->roles;

// send email when membership is first approved, i.e. from subscriber to s2member_level1
if( in_array( 'subscriber', $old_roles ) && in_array( 's2member_level1', $new_roles ) ) {
	$to = $user_info->user_email;
	$subject = 'Welcome to the ' . get_bloginfo ( 'name' ) . ' website!';
	$message = 'Hello, ' . $user_info->display_name . '!' "\r\n\r\n";
	$message = 'Your membership registration has now been approved on the ' .$site_url. ' website.' "\r\n";
	wp_mail( $to, $subject, $message );
}
}
add_action( 'profile_update', 'kts_user_role_approve_email', 10, 2 );

I very much doubt it. It only works if the member was a subscriber and then becomes a Level 1 member. So it won’t work for a new registration. I suspect you have something else sending out emails for that.

Sorry if i confused you Tim, i have edited my answer above. it’s the original code i have pasted that’s sending the same notification of a new user registers… Anyway, i’m checking your code, i’m no programmer but i understand a little bit of it.
There’s seems to be an error.

Try removing the "\r\n\r\n" and "\r\n" (but leave the ; that follows them).

1 Like

At last! Worked perfectly! Thanks much Tim!

1 Like