How to Configure Zabbix to Send Email Alerts to Gmail Account – Part 2

If you are using Zabbix to monitor your infrastructure you might want to receive email alerts from your local domain somewhere on a public internet domain, even if you don’t own a valid registered internet domain name with a mail server which you can configure on your own.

This tutorial will briefly discuss how to set up a Zabbix server to send mail reports to a Gmail address by using the SSMTP program, without the need to install and configure any local MTA daemon, such as Postfix, Exim, etc.

Requirements

Step 1: Install and Configure SSMTP

1. SSMTP is a small software, which does not fulfill any of the functionality of a mail server, but only delivers emails from a local machine to an external email address on a mailhub.

To install the SSMTP program alongside with mailutils package that you will use to send mails, issue the following command on your RedHat-based distros and Debian like server:

# yum install msmtp mailx               [On RHEL/CentOS] 
$ sudo apt-get install ssmtp mailutils       [On Debian/Ubuntu]

2. After the packages are installed on the system, configure the SSMTP program to send local emails to your Gmail account by opening the main configuration file for editing with your favorite text editor and root privileges and use the following parameter settings:

# vi /etc/msmtprc                       [On RHEL/CentOS]
$ sudo nano /etc/ssmtp/ssmtp.conf            [On Debian/Ubuntu]

MSMTP settings for GMAIL account.

Configuration of /etc/msmtprc
#set default values for all following accounts.
defaults
auth           on
tls            on
tls_trust_file    /etc/pki/tls/certs/ca-bundle.crt
logfile        ~/.msmtp.log
# Gmail
account        gmail
host           smtp.gmail.com
port           587
from           [email protected]
user           [email protected]
password       gmailpassword

# Set a default account
account default : gmail

SSMTP settings for GMAIL account.

Configuration of /etc/ssmtp/ssmtp.conf
[email protected]
mailhub=smtp.gmail.com:587
rewriteDomain=your_local_domain
hostname=your_local_FQDN
UseTLS=Yes
UseSTARTTLS=Yes
AuthUser=Gmail_username
AuthPass=Gmail_password
FromLineOverride=YES
Configure Zabbix Email Alerts
Configure Zabbix Email Alerts

Step 2: Gmail Tests for Zabbix Email Alerts

3. On the next step it’s time to send a locally generated email to a Gmail account by issuing the below command.

# echo "Body test email from 'hostname -f' "| mail -s "subject here" [email protected]
Gmail Tests
Gmail Tests

4. Normally, Gmail prevents different types of authentications to their servers from your account, so, in case you get the error “mail: cannot send a message: Process exited with non-zero status”, then login to your Gmail account from the browser and navigate to the following link https://www.google.com/settings/security/lesssecureapps in order to allow access for less secure apps as in the following screen.

Manage Secure Gmail Apps
Manage Secure Gmail Apps

5. After you have turned on the Less Secure Apps feature on your Gmail account, run the above mail command again and verify your Inbox after a few seconds to check if the locally generated email has been successfully delivered – you should normally see the email has incoming from Gmail.

Mail Delivery Confirm
Mail Delivery Confirm

Step 3: Configure Zabbix Sendmail Script

6. Further, based on the $(which mail) command creates the following Bash script to Zabbix alertscripts directory with the following content and gives it execute permissions:

# vi /usr/local/share/zabbix/alertscripts/zabbix-sendmail            [On RHEL/CentOS]
$ sudo nano /usr/local/share/zabbix/alertscripts/zabbix-sendmail     [On Debian/Ubuntu]

Script content:

#!/bin/bash
echo "$3" | /usr/bin/mail -s "$2" $1
Configure Sendmail Zabbix
Configure Sendmail Zabbix

Next, set the execute permission on the script file.

# chmod +x /usr/local/share/zabbix/alertscripts/zabbix-sendmail

7. Next, as previously, test the script functionality by sending a local email to a Gmail account. The way to run the script with positional parameters is explained above:

# /usr/local/share/zabbix/alertscripts/zabbix-sendmail [email protected] "Subject here" "Body of the message here"
Send Mail to Gmail Account from Linux
Send Mail to Gmail Account

Afterward, verify Gmail Inbox and check if the new local message has arrived.

Verify Mail Delivery
Verify Mail Delivery

Step 4: Configure Zabbix to Send Alerts to Gmail

8. If the tests so far we’re successful, then you can move to the next step and set up Zabbix to send generated email alerts to Gmail. First, log in to the Zabbix web interface and navigate to the following menu: Administration -> Media types -> Create media type.

Zabbix Administration
Zabbix Administration

9. On the next screen enter an arbitrary Name to uniquely identify the script in the Zabbix configurations (in this example Send-Email-Script is used), choose Script as Type from the list and enter the name of the Bash script created earlier (zabbix-sendmail used in this tutorial) to send email from the command line (don’t use the path for the script, only the script name). When you’re done, hit the Add button below to reflect changes.

Create Zabbix Email Alerts
Create Zabbix Email Alerts

10. Further, let’s configure an email address to which you will send Zabbix alerts. Go to Profile -> Media -> Add and a new pop-up window should appear.

Here, select the name of the script that you have earlier named (in this example Send-Email-Script is used) for Type, enter the Gmail address to which you will send emails, choose the time period (week, hours) when email reports should be active for sending, choose the severity of the messages that you want to receive on your Gmail address, select Enabled as Status and hit the Add button to add the media. Finally hit the Update button to apply the configuration.

Configure Zabbix Mail Address
Configure Zabbix Mail Address
Zabbix Update Configuration
Zabbix Update Configuration

11. On the next step, enable the default Zabbix alerts by navigating to Configuration -> Actions, select as the Event Source – > Triggers from the right menu, and hit on Disabled Status in order to enable it. Repeat the step for Event Source – > Internal or other custom-created Actions and you’re done.

Enable Default Zabbix Mail Alert
Enable Default Zabbix Mail Alert
Zabbix Enabled Actions
Zabbix Enabled Actions

Wait for a while for Zabbix to start to gather information and generate some reports, then verify your Gmail Inbox and you should see some Zabbix alerts submitted so far.

Zabbix Monitoring Mail Alerts
Zabbix Monitoring Mail Alerts

That’s all! Although this guide was mainly focused on sending Zabbix alerts to a Gmail account using Gmail SMTP server as a mailhub, using the same configuration you can, also, push Zabbix email alerts further to other valid internet email accounts by relying on Gmail to route your emails through SMTP servers.

Matei Cezar
I'am a computer addicted guy, a fan of open source and linux based system software, have about 4 years experience with Linux distributions desktop, servers and bash scripting.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

52 thoughts on “How to Configure Zabbix to Send Email Alerts to Gmail Account – Part 2”

  1. Good Evening…

    Please we do not understand how you could make the script file.

    # vi /usr/local/share/zabbix/alertscripts/zabbix-sendmail [On RHEL / CentOS 7]
    $ sudo nano/usr/local/share/zabbix/alertscripts / zabbix-sendmail [On Debian 8]
    
    Reply
    • Hello,

      I also was not able to find that file on that location. Please check the server conf file at /etc/zabbix/zabbix_server.conf and look for ‘alertscripts‘ path.

      My default folder was /usr/lib/zabbix/alertscripts for some reason.

      Reply
  2. I stumbled onto this howto and was mildly irritated by the use of SSMTP but then for anybody using RedHat it might be harder to install the alternative Nullmailer.

    In systems of the Debian family-like Raspi and Ubuntu, both are available. Nullmailer should be preferred because it is currently still maintained. In Red Hat’s descendants, you are more likely to find SSMTP. It has not been changed since 2014, although there are security concerns afaik.

    While SSMTP dumps messages directly to the configured SMTP server and terminates immediately, Nullmailer works like a “real” mail server: The program uses a queue into which it accepts messages and which it processes regularly. Accordingly, a Nullmailer process runs permanently. Sending via Nullmailer is, therefore, more robust. SSMTP is more suitable as an extra in containers.

    Reply
  3. Hi! The issue is: we are getting trigger alert emails as downloadable files. We need to download a file and then only we can see what is inside that file. We want alert as texts. Can anyone please help us out regarding this.

    Reply
  4. Could not get the old command to work with Office365. Try this one instead (don’t forget to add the command line arguments in the Zabbix dashboard)

    /usr/bin/mail -s "$2" -r "HARDCODE_FROM_ADDRESS" "$1" <<< "$3"

    Reply
  5. Hey Hi,

    I have followed the procedure up to the allow access for less secure apps in my gmail account following your indications.But I am getting the error like this

    # echo “Body test email from ‘hostname -f’ “| mail -s “subject here” [email protected]
    O/P : mail: cannot send message: Process exited with a non-zero status

    can you please help me on this issue.

    Reply
  6. Hi,

    I’m using Ubuntu and alertscript dir exists in /usr/lib/zabbix/alertscripts/. So I’ve make script file zabbix-sendmail in this location.

    I’ve using my other smtp server instead of Gmail which is working fine.

    I’ve completed step 3 and successfully able to send mail using /usr/lib/zabbix/alertscripts/zabbix-sendmail Mail-id “Subject” “Body” this command.

    Now there is issue in setp 4. I’ve configured media for sending mail and also enable action. But now triggers changed in zabbix GUI but it not able to send mail even no any logs are coming.

    So please suggest me what should I need to check ?

    Reply
      • I’ve check in /etc/zabbix/zabbix_server.conf file and in file alert location mention as below:

        AlertScriptsPath=/usr/lib/zabbix/alertscripts
        

        It means script location is /usr/lib/zabbix/alertscripts and I’ve also create script in on same location but not get success.

        Reply
  7. alertscripts folder name and location is in zabbix_server.conf, where zabbix_server.conf is in /etc/zabbix default location for alertscripts in Ubuntu is /etc/zabbix/alert.d everything else worked, thanks

    Reply
  8. Hi, I follow the process up to allow access for less secure apps in my gmail account following your indications. However after apply changes and confirme it is turned on. I still receive the error ail: cannot send message: Process exited with a non-zero status.

    Any Idea about what could be the problem?

    thanks in advance.

    Reply
  9. I found one thing missing: on Centos 7, ssmtp does not automatically activate itself as default mta when installed. I had to do command:

    # alternatives --config mta
    

    and then choose the number next to ssmtp. Before that postfix was grabbing my emails, so they weren’t being routed through gmail.

    Reply
  10. Hi,

    I followed all the guides regarding sending mail using Gmail,

    I manage to send test mails via script to Gmail accounts, but through zabbix mails are stuck in “in progress, 3 retries left” status.

    Any help would be appreciated.

    Muli

    Reply
    • @Joseph,

      Thanks for notifying us about latest release of Zabbix 3.2.x, yes we will update the article to meet latest version, till then stay tuned to Tecmint.

      Reply
  11. Hi i am using 2.4.8 version till step: 3, I able to see the mail notifications to my gmail account. In step:4 I have done the same steps but i unable to receive the mails. Zabbix not sending emails reporting msg “status: not sent. Can some one please help me.

    Reply
  12. Since Zabbix 3.0 you must configure the Media Type to pass the parameters manually, they are not passed automatically as $1, $2 and $3 anymore so you need to add three parameters to the Media Type manually:

    {ALERT.SENDTO}
    {ALERT.SUBJECT}
    {ALERT.MESSAGE}
    

    if you don’t fix the media type like this, the script wont do anything.

    Reply
  13. I have installed zabbix and done your configuration to send email, but when in your instruction you pass the $1, $2 and $3 parameters to the mail script?

    Reply
  14. I have the same error.

    My Zabbix tell sending emails reporting msg “status: sent”.
    but i cant see the mail
    Can u help me?

    sorry for my english.

    Reply
    • It looks like your mail server is not configured propery to send or forward mails. Invastigate this issue further.

      Reply
  15. Hi,
    I’m testing using command line and its works fine.
    But my Zabbix not sending emails reporting msg “status: not sent”.
    Can u help me?
    Tnx

    Reply
    • It’s the same problem I have now. Sending email via command line works but not in Zabbix. Did you come up with a solution or a way to trace where the problem is?

      Thanks!

      Reply
  16. thank you for this post helpful
    please can you help me to integrate zabbix with request tracker or OTRS or other like this
    thank u in advance

    Reply
  17. One error that i got was the path of the script, to get it working on my zabbix 3.0, i had to change the path from /usr/local/share/zabbix/alerscripts/zabbix-sedmail to /usr/lib/zabbix/alertscripts/zabbix-sendmail.

    Hope it helps!

    Reply
    • I installed zabbix 3.0 on centos7, i also created zabbix-sendmail file in /usr/lib/zabbix/alertscripts/ because /usr/local/share/zabbix/alerscripts/ path does not exist.

      I configured ssmtp according this documentation, now i am getting mail by command line but zabbix not sending reports. Action and internal are all enable.

      Please help

      Reply
    • @Andrews,

      How you solved problem yourself? it would be great if you could mention those instructions so that its will beneficial to others, who face similar problem…

      Reply
  18. Good article, Thx.
    I’m having a problem when trying to send an email using /etc/zabbix/scripts/sendemail.sh a*******[email protected] “Test” “Msg Test”.
    /etc/zabbix/scripts/sendemail.sh: line 2: /usr/bin/mail: No such file or directory
    Can u help me?
    Cheers

    Reply
  19. Hi
    After running “yum install ssmtp mailx” on centos 7
    I can’t find /etc/ssmtp/ssmtp.conf
    the file does not exist.

    can you please help?

    Reply
    • @Itai,

      That means both the packages ssmtp and mailx are not installed on the system, if you think you’ve managed to install them successfully, could you check them using following command, if installed below command will show output as shown.

      # rpm -q ssmtp mailx
      
      ssmtp-2.64-14.el7.x86_64
      mailx-12.5-12.el7_0.x86_64
      
      Reply
  20. Thanks for the walkthrough! When I configure SSMTP and run it by itself to test, it works fine. When I run the script listed in step seven (zabbix-sendmail), it fails with “mail: cannot send message: Process exited with a non-zero status”. /var/log/mail.err gives the error “Client does not have permissions to send as this sender”. I have given chmod +x to the script, and have ran it using sudo and without. Any idea what is going on?

    Reply
  21. You’re right! If you install Zabbix from repositories using binary packages the path for scripts would be /usr/lib/zabbix/alertscripts/, else, if you compile and install it from sources, the path is /usr/local/share/zabbix/alertscripts/. Once again… this tutorial covers only Zabbix installation from sources, but your remark is welcome for users who tend to install zabbix server using distribution repositories.

    Reply
  22. Very good article. It works for me!!! Thx a lot!!
    Just a small comment/question.
    The path to create the script “zabbix-sendmail” I suppose that might be same path set in zabbix_server.conf with the parameter “AlertScriptsPath”

    For the standard install of zabbix server, this parameter is set to AlertScriptsPath=/usr/lib/zabbix/alertscripts
    Instead of /usr/local/share/zabbix/alertscripts/

    Cheers

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.