How to Fix “Monit Email Alert Not Working with Gmail SMTP Configuration”


29 views

When configuring Monit to send email alerts through Gmail's SMTP server, several authentication and connection issues can occur. The error message "error receiving data from the mailserver 'smtp.gmail.com' -- Resource temporarily unavailable" typically indicates one of these underlying problems:

// Typical error pattern seen in monit logs
Sendmail: error receiving data from the mailserver 'smtp.gmail.com'
Alert handler failed, retry scheduled for next cycle

Here's a verified configuration that works with modern Gmail requirements:

set mailserver smtp.gmail.com port 587
    username "your.email@gmail.com" password "your-app-password"
    using tlsv1
    with timeout 30 seconds
    using hostname "smtp.gmail.com"

set mail-format {
    from: your.email@gmail.com
    subject: $SERVICE $EVENT at $DATE
    message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
}

set alert recipient@domain.com
  • Use an App Password instead of your regular Gmail password if you have 2FA enabled
  • Ensure TLSv1 or higher is specified (TLSv1.2 recommended)
  • Verify your firewall isn't blocking outbound port 587
  • Check Google Account security settings for "Less secure app access" if not using App Password

Test your SMTP configuration independently using this command:

echo "Test email body" | mailx -s "Test Subject" -S smtp="smtp.gmail.com:587" \
-S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="your.email@gmail.com" \
-S smtp-auth-password="yourpassword" recipient@example.com

If this test fails, the issue lies with your SMTP setup rather than Monit specifically.

If email continues to be problematic, consider these alternatives:

# Slack webhook example
check file alerttest with path /.nonexistent
    if does not exist then exec "/usr/bin/curl -X POST -H 'Content-type: application/json' \
    --data '{\"text\":\"Alert: file missing\"}' https://hooks.slack.com/services/your/webhook"

When configuring Monit with Gmail's SMTP server, developers often encounter these specific error patterns:

Sendmail: error receiving data from the mailserver 'smtp.gmail.com' -- Resource temporarily unavailable
Alert handler failed, retry scheduled for next cycle

Here's the full working configuration that addresses multiple potential failure points:

set mailserver smtp.gmail.com port 587
    username "your.email@gmail.com" password "app-specific-password"
    using tlsv1
    with timeout 30 seconds
    using hostname "yourdomain.com"  # Important for some Gmail configurations

set alert your.email@domain.com

1. App Password Requirement: Since May 2022, Google requires app passwords for third-party apps:

  • Enable 2FA on your Google account
  • Generate an app password specifically for Monit

2. Firewall Considerations: Ensure outgoing connections to port 587 aren't blocked:

sudo ufw allow out 587/tcp  # For Ubuntu systems

To isolate the problem, test SMTP connectivity directly:

openssl s_client -connect smtp.gmail.com:587 -starttls smtp -crlf

Expected response should include:

250 SMTPUTF8

If standard configuration fails, try these variations:

# Option 1: Explicit TLS declaration
set mailserver smtp.gmail.com port 587
    username "user@gmail.com" password "pass"
    using tls
    with timeout 30 seconds

# Option 2: Different TLS version
using tlsv1_2

Check Monit's mail queue status:

monit status

Look for entries like:

Mail queue status: 1 message(s) pending

After configuration changes:

  1. Reload Monit: monit reload
  2. Force immediate check: monit monitor alerttest
  3. Check logs: tail -f /var/log/monit.log