how to run loop in function.php that sends email based on specific conditions?

The post’s data is passed as an argument to the function hooked to transition_post_status, so you don’t need to query for anything (also, your query is a bit strange, using both WP_Query and query_posts, but that’s a separate matter). Here’s a trimmed version of your function showing the important changes:

function a_new_post( $new_status, $old_status, $post ){
    if ( 'publish' !== $new_status or 'publish' === $old_status )
        return;

    $posttags = get_the_terms( $post->ID, 'post_tag' );
    if( $posttags ) {
        $blogusers = get_users();
        foreach( $posttags as $tag ) {
            // the rest of your code
        }
    }
}

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)