Notify users whenever a new post is published based on their preference

I have put together a custom registration form on my WordPress site and one of the items on the registration form is an ability to select an interest.

Essentially I have the following code from the registration form…

 update_usermeta( $user_id, 'interests', $_POST['interests'] );

I would need a code that does something like, if “interestes” matches “tag” of a new post, then send an email to the user.

Anything like that possible? Thank you in advance!

Solutions Collecting From Web of "Notify users whenever a new post is published based on their preference"

See this example on the question 19259.

The function was init on publish a post, change the hook to your post type or use on default post – Hook publish_post. Before you send check in the function your custom user meta field, like get_user_meta( $user_id, 'interests', TRUE ); and ready to send the information about new post.

Maybe liek this solution, but untested!

    add_action( 'publish_post', 'fb_send_mail_about_new_post' );
    function fb_send_mail_about_new_post( $post_id = FALSE ) {

        // check user meta data, if interest
        $interests = FALSE;
        $interests = get_user_meta( get_current_user_id(), 'interests', TRUE );
        if ( ! $interests )
            return NULL;

        if ( $post_id ) {
            // get data from current post
            $post_data = get_post( $post_id );
            //var_dump($post_data);exit; <-- see this for all content or use the id for get the revisons

            // get mail from author
            $user_mail = get_userdata( $post_data -> post_author );

            // email addresses
            $to = get_user_meta( get_current_user_id(), 'email', TRUE );
            // email subject
            $subject = get_option( 'blogname' ) . ': ' . $post_data -> post_title;
            // message content
            $message = $post_data->post_content . ' ' . PHP_EOL . 
                get_author_name( $post_data->post_author ) . ' ' . PHP_EOL . 
                get_permalink( $post_id );
            // create header data
            $headers = 'From: ' . 
                get_author_name( $post_data -> post_author ) . 
                ' (' . get_option( 'blogname' ) . ')' . 
                ' <' . $user_mail -> user_email . '>' . 
            // send mail

        return $post_id;

A other alternative way is to use a plugin and leave many time, like this free plugin Inform about Content.
The plugin add two fields to the user profile for update of post and comment via mail. You can enhance with a custom plugin to the register site for new users to fill up this fields.

Why reinvent the wheel? You should be able to hook into wp_update_user and then add the users email/remove it via a plugin like subscribe2 based on the value of that field.

If you want a more custom solution you could create your own plugin and do something like:

    class emailer {
  function send($post_ID)  {
    $friends = ',';
    mail($friends,"sally's blog updated",'I just put something on my blog:');
    return $post_ID;
$myEmailClass = new emailer();
add_action('publish_post', array($myEmailClass, 'send'));

Which is copy and pasted from the Plugin API

This would have to be modified with your if statement and a for-each but other than that should work. I would recommend just going with the first option though.