Moodle Mail Smtp: WTF!

PROBLEM: Moodle not sending mails when users post on (courses) forums.

The fix is applied to a Moodle (ver 3.1) installation hosted on a shared server (using cpanel).

Wrong approachaaa!

Usually, the whole problem-solving process seems to focus mainly on finding the right mail settings through this steps:

Post on forums, check if mail is received.
If no mails are received, change mail settings and repeat step one until “You got mail!”.

In my experience, that’s the wrong way.

Let’s try instead to go to moodle admin notification page first and run a cronjob.
In our case, despite we were using the right mail setting configuration, we were not receiveing mails… until… we run Cron.

When Cron was run, we received (through mail) all the unsent forum posts (so until that moment we were not even really able to test the right mail setting configuration, because even with the right confs, mails are not sent if cron isn’t run).

In fact, if You read below, as reported in Moodle Docs:

The Moodle ‘cron’ process… must be run regularly in the background.
The Moodle cron script runs different tasks at differently scheduled intervals.

IMPORTANT: Do not skip setting up the cron process on your server for your Moodle. Your site will not work properly without it. A special program (typically called – not surprisingly – ‘cron’) is used to run the Moodle cron script at a regular interval.
The Moodle cron script runs tasks include sending mail, updating Moodle reports, RSS feeds, activity completions, posting forum messages and other tasks…

Now the Right approach.

First, i suggest to run cron each time You change the mail settings to get a real feedback and consequently being sure if they are right, this way:

Post on forums, run cronjob, check if mail is received.
If no mails are received, change mail settings and repeat step one until “You got mail!”.

Once You’ve found the right mail settings and You’re sure that users can receive forum posts through mail (each time You run cron), we just need to tell the Cron program to run automatically each 30 minutes (the moodle default time deadline for editing content of forum posts) .

So, look for the “admin” directory of Your Moodle installation.

Usually on a shared hosting – assuming Your server user name is “YOUR_USER”, and Your domain name is “YOURWEBSITE.COM” (then You just need to replace the UPPERCASE parts of the line) – the path should look like this:

/home/YOUR_USER/public_html/YOURWEBSITE.COM/admin/

or

/home/YOUR_USER/public_html/YOURWEBSITE.COM/moodle/admin/

Now, in the way You prefere (we have Cpanel and the fix also depends on Your server admin panel), add a Cronjob that runs every 30 minutes pointing to the file: “/admin/cli/cron.php”.

CPANEL

On Cpanel You’ll do this going to Cpanel > Cronjobs and adding a new Cron command (in our case our server runs php56, so first check which php version is running on Yours) that should look like (still need to replace the UPPERCASE):

php56 /home/YOUR_USER/public_html/YOURWEBSITE.COM/admin/cli/cron.php

or

php56 /home/YOUR_USER/public_html/YOURWEBSITE.COM/moodle/admin/cli/cron.php

Lastly, if you don’t want to receive emails every time a cronjob is run by the server, use this command:

php56 /home/YOUR_USER/public_html/YOURWEBSITE.COM/admin/cli/cron.php > /dev/null

I hope it helps…

FixThatCode!