Email Settings on Test and Development
There are a number of settings provided by Moodle for test and development environments specifically to avoid issues around sending emails to users from a non-Production site which is never a good look! This is a common trap for environments that are refreshed from producton, particularly when running cron/scheduled tasks
Firstly there is the all or nothing setting. If set, no emails will ever be sent.
$CFG->noemailever = true;
Secondly there is the combination of the $CFG->divertallemailsto
and $CFG->divertallemailsexcept
settings which will divert all emails to a specified address, unless they are on the exception list which can be defined with a regular expression (regex):
Here's a simple example:
$CFG->divertallemailsto = 'noreply@moodlewiki.com';
$CFG->divertallemailsexcept = '[A-Za-z0-9._%+-]+@moodlewiki.com';
This will divert all emails to noreply@moodlewiki.com
unless they match the address @moodlewiki.com
in which case it will send them as normal.
You can see this in action with the following RegExr demo.
Notice how the Regex only matches @moodlewiki.com addresses and ignores the others? Only matches to the regex will get emails, all others are diverted to the catch all address.
So make sure all your non-production environments have both the $CFG->divertallemailsto
and $CFG->divertallemailsexcept
settings in place in their configuration files. This is probably the simplest and most reliable way to prevent emails being sent by accident through Moodle as it doesn't require any database updates, hacks, or 3rd party plugins.
No Comments