diff --git a/lang/en/error.php b/lang/en/error.php
index 7530dedd2f9..874ecbd2691 100644
--- a/lang/en/error.php
+++ b/lang/en/error.php
@@ -391,6 +391,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 4d758b1b5b1..dd6bea6199f 100644
--- a/message/classes/api.php
+++ b/message/classes/api.php
@@ -1699,6 +1699,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) [