Skip to content

Instantly share code, notes, and snippets.

@superdaigo
Created September 20, 2012 04:58
Show Gist options
  • Star 80 You must be signed in to star a gist
  • Fork 45 You must be signed in to fork a gist
  • Save superdaigo/3754055 to your computer and use it in GitHub Desktop.
Save superdaigo/3754055 to your computer and use it in GitHub Desktop.
Zabbix SMTP Alert script for gmail
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Zabbix SMTP Alert script for gmail.
"""
import sys
import smtplib
from email.MIMEText import MIMEText
from email.Header import Header
from email.Utils import formatdate
# Mail Account
MAIL_ACCOUNT = 'your.account@gmail.com'
MAIL_PASSWORD = 'your mail password'
# Sender Name
SENDER_NAME = u'Zabbix Alert'
# Mail Server
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
# TLS
SMTP_TLS = True
def send_mail(recipient, subject, body, encoding='utf-8'):
session = None
msg = MIMEText(body, 'plain', encoding)
msg['Subject'] = Header(subject, encoding)
msg['From'] = Header(SENDER_NAME, encoding)
msg['To'] = recipient
msg['Date'] = formatdate()
try:
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
if SMTP_TLS:
session.ehlo()
session.starttls()
session.ehlo()
session.login(MAIL_ACCOUNT, MAIL_PASSWORD)
session.sendmail(MAIL_ACCOUNT, recipient, msg.as_string())
except Exception as e:
raise e
finally:
# close session
if session:
session.quit()
if __name__ == '__main__':
"""
recipient = sys.argv[1]
subject = sys.argv[2]
body = sys.argv[3]
"""
if len(sys.argv) == 4:
send_mail(
recipient=sys.argv[1],
subject=sys.argv[2],
body=sys.argv[3])
else:
print u"""requires 3 parameters (recipient, subject, body)
\t$ zabbix-gmail.sh recipient subject body
"""
@afield1235
Copy link

@sgjava
I followed those steps and it worked for Office365. Make sure that each script parameter is on its own line. I accidentally tried copying and pasting all of them into one parameter line which doesn't work.

@raghusundaram
Copy link

I was also been experiencing the issue of not receive the alert mail.
I have done two things which solved the problem.

I have added the script parameters as suggested by @bennetraja
I have also set the media type name just as Script. not as the actual script name.

I have just created the account to share my findings here.
I am more than happy to help for those who still couldn't crack it after seeing this post.

@victortorrescosta
Copy link

It works!!! =D
Don't forget to add the script parameters as pointed about above!!! (follow sgjava's comment)

@sgjava
Copy link

sgjava commented Jul 10, 2018

OK folks, my Zabbix server sharted, so I had to rebuild it. A little different this time with ODROID XU4, mainline kernel, Ubuntu 18.04, Zabbix Server 3.4.11 and Python 3 (Python 2 not installed by default). Hopefully my loss is your gain :) Since the original script doesn't work on Python 3 you'll need to edit that. I was too lazy to do a PR :)

#!/usr/bin/env python3

from email.mime.text import MIMEText
from email.header import Header
from email.utils import formatdate

print(u'requires 3 parameters (recipient, subject, body) \t$ zabbix-gmail.sh recipient subject body')

The rest of the stuff in my previous post is still relevant.

Test with:
sudo -H -u zabbix python3 /usr/local/share/zabbix/alertscripts/zabbix-alert-smtp.py yourgmail@gmail.com test testbody

@CESARAUGUSTOALONKA
Copy link

Great job. This script works fine with Zabbix 4.0.5 running on Centos 7.6.1810

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment