How to Configure Monit to Send Email Alerts with SendGrid

Monit is my favorite tool for monitoring WordPress servers. I use Monit to make sure all of my crucial processes like nginx or Apache is always running among other things. Monit will also send you email alerts. For maximum security you should not host an email server by yourself. There are multiple free email services like SendGrid that will allow you to send Monit alerts by SMTP when server load is high or a service crashes.

This guide assumes you have already installed and configured Monit.

Configure Monit to Send Email Alerts with SendGrid

You will need your SendGrid API Key from your account.

To create one go to Settings > API Keys and click Create API Key > General API Key.

Name the key.

Make sure the Mail send privilege has Full Access.

Click Save.

sendgrid-add-api-key

You will only be shown the SendGrid API Key once so copy it somewhere safe.

sendgrid-apikey-show

Open your Monit configuration

sudo nano /etc/monit/monitrc

Add this section to your Monit so it will use SendGrid to send emails via SMTP when there is an alert from Monit.

The from line is where the email will appear to come from ([email protected]) so you should include your domain there

For the password use your Sendgrid api key for insert-sendgrid-api-key-here and keep it wrapped in ""

For the set alert section set the email address you wish to receive alerts, here it is [email protected]

#Mail settings
 set mail-format {
     from: [email protected]wp-bullet.com
  subject: monit alert --  $EVENT $SERVICE
  message: $EVENT Service $SERVICE
                Date:        $DATE
                Action:      $ACTION
                Host:        $HOST
                Description: $DESCRIPTION

           Your faithful employee,
           Monit }
  set mailserver smtp.sendgrid.net port 587
     username "apikey" password "insert-sendgrid-api-key-here"
     using TLSV1 with timeout 30 seconds
  set alert [email protected] #email address which will receive monit alerts

Ctrl+X, Y and Enter to Save and Exit

Test your Monit configuration syntax

sudo monit -t

You should see this status message, if you see nothing that is also OK.

Control file syntax OK

Now reload Monit

sudo service monit reload

You can log in to your Monit web interface and restart a service, you should get an email alerting you shortly afterwards.

Sources

SendGrid SMTP