diff --git a/lib/moodlelib.php b/lib/moodlelib.php index ae5e137840f..57bc5d859f9 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -5028,6 +5028,17 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $a return true; } + if (!validate_email($user->email)) { + // we can not send emails to invalid addresses - it might create security issue or confuse the mailer + $invalidemail = "User $user->id (".fullname($user).") email ($user->email) is invalid! Not sending."; + error_log($invalidemail); + if (CLI_SCRIPT) { + // do not print this in standard web pages + mtrace($invalidemail); + } + return false; + } + if (over_bounce_threshold($user)) { $bouncemsg = "User $user->id (".fullname($user).") is over bounce threshold! Not sending."; error_log($bouncemsg);