# Tutor LMS: Email the Instructor When a Quiz Is Submitted

> Emails the course author each time a student finishes a Tutor LMS quiz, with the score, so instructors can follow up or grade open questions quickly.

- Type: PHP
- Runs: Run everywhere
- Category: [Courses & Community](https://wpide.com/library/category/learning/)
- Tags: tutor, quizzes, email
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/tutor-quiz-attempt-email-instructor/

## Code

```php
if ( ! function_exists( 'tutor_utils' ) ) {
    return;
}

add_action( 'tutor_quiz/attempt_ended', function ( $attempt_id, $course_id = 0, $user_id = 0 ) {
    $attempt = tutor_utils()->get_attempt( $attempt_id );

    if ( ! $attempt ) {
        return;
    }

    $course_id = $course_id ? (int) $course_id : (int) ( $attempt->course_id ?? 0 );
    $student   = get_userdata( $user_id ? (int) $user_id : (int) ( $attempt->user_id ?? 0 ) );
    $author    = get_userdata( (int) get_post_field( 'post_author', $course_id ) );

    if ( ! $student || ! $author ) {
        return;
    }

    $total = (float) ( $attempt->total_marks ?? 0 );
    $score = $total > 0 ? round( (float) $attempt->earned_marks / $total * 100 ) : 0;

    wp_mail(
        $author->user_email,
        sprintf( 'Quiz submitted: %s', get_the_title( (int) $attempt->quiz_id ) ),
        sprintf( "%s finished \"%s\" in \"%s\" with a score of %d%%.\n\nReview attempts: %s", $student->display_name, get_the_title( (int) $attempt->quiz_id ), get_the_title( $course_id ), $score, admin_url( 'admin.php?page=tutor_quiz_attempts' ) )
    );
}, 10, 3 );
```

## Notes

The email goes to the author of the course the quiz belongs to.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Tutor LMS: Email the Instructor When a Quiz Is Submitted" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
