According to the official Moodle documentation, you need to make sure the correct port is set. Especially if you are using SSL:
Most SMTP hosts make use of the default port 25 to for listening email
traffic. However, occasionally a non-default port is used. In such
cases, entering an SMTP host followed by a colon (i.e. :) and the port
number should work. For example, if my SMTP host is myemailserver.com
and it listens on port 587 or 465 (most common) instead of port 25 I
would enter the SMTP host as myemailserver.com:587.
Looking at the official Google document entitled “Google Apps SMTP settings to send mail from a printer, scanner, or app” shows more info such as the SMTP port for Gmail being 465.
So knowing those two bits of information it seems to me that you should be changing the SMTP host from this:
smtp.gmail.com
To this:
smtp.gmail.com:465
And try again.