diff --git a/lang/en/error.php b/lang/en/error.php
index 1c50771e6d4..749cd1000d9 100644
--- a/lang/en/error.php
+++ b/lang/en/error.php
@@ -389,6 +389,7 @@ $string['loginasonecourse'] = 'You cannot enter this course.
You have to t
$string['maxbytesfile'] = 'The file {$a->file} is too large. The maximum size you can upload is {$a->size}.';
$string['maxareabytes'] = 'The file is larger than the space remaining in this area.';
$string['messagingdisable'] = 'Messaging is disabled on this site';
+$string['messageundeliveredbynotificationsettings'] = 'Message failed to deliver. Please check the notification settings.';
$string['mimetexisnotexist'] = 'Your system is not configured to run mimeTeX. You need to obtain the C source from https://www.forkosh.com/mimetex.zip, compile it and put the executable into your moodle/filter/tex/ directory.';
$string['mimetexnotexecutable'] = 'Custom mimetex is not executable!';
$string['missingfield'] = 'Field "{$a}" is missing';
diff --git a/message/classes/api.php b/message/classes/api.php
index a9a6675dfe0..e3d830055e7 100644
--- a/message/classes/api.php
+++ b/message/classes/api.php
@@ -2010,6 +2010,10 @@ class api {
$messageid = message_send($eventdata);
+ if (!$messageid) {
+ throw new \moodle_exception('messageundeliveredbynotificationsettings', 'moodle');
+ }
+
$messagerecord = $DB->get_record('messages', ['id' => $messageid], 'id, useridfrom, fullmessage,
timecreated, fullmessagetrust');
$message = (object) [