2828import string
2929import sys
3030
31+ import six
32+
3133
3234def usage ():
3335 print ("""
@@ -36,30 +38,30 @@ def usage():
3638 Zabbix setting: 'Administration' -> 'Media types'
3739 https://hostname/zabbix.php?action=mediatype.edit&mediatypeid=4
3840 Script parameters: {ALERT.SENDTO} {ALERT.SUBJECT} {ALERT.MESSAGE}
39- Example: python %s "dinggd @example.com " "Test email from Python" "Python rules them all!"
40- """ ) % (__file__ , sys .argv [0 ])
41+ Example: python %s "admin @example.domain " "Test email from Python" "Python rules them all!"
42+ """ % (__file__ , sys .argv [0 ]) )
4143 sys .exit (0 )
4244
4345
4446EMAIL_HOST = "smtp.mxhichina.com"
47+ EMAIL_PORT = 25 # default smtp port
4548EMAIL_HOST_USER = 'sender@example.com'
4649EMAIL_HOST_PASSWORD = 'password'
4750EMAIL_HOST_ENABLE_SSL = True
4851EMAIL_HOST_ENABLE_TLS = False
4952
50- if EMAIL_HOST_ENABLE_TLS :
51- EMAIL_PORT = 587
52- elif EMAIL_HOST_ENABLE_SSL :
53+ if EMAIL_HOST_ENABLE_SSL and EMAIL_HOST_ENABLE_TLS :
54+ raise Exception ("can NOT be used together" )
55+
56+ if EMAIL_HOST_ENABLE_SSL :
5357 EMAIL_PORT = 465
54- else :
55- EMAIL_PORT = 25
5658
5759DEFAULT_FROM_EMAIL = EMAIL_HOST_USER # https://tools.ietf.org/html/rfc822.html#appendix-A
5860CRLF = "\r \n " # for Windows user
5961
6062EMAIL_TO = "receiver@example.com" # user defined variable, in Zabbix is {ALERT.SENDTO}
61- SUBJECT = "Test email from Python" # user defined variable, in Zabbix is {ALERT.SUBJECT}
62- text = "Python rules them all!" # user defined variable, in Zabbix is {ALERT.MESSAGE}
63+ EMAIL_SUBJECT = "Test email from Python" # user defined variable, in Zabbix is {ALERT.SUBJECT}
64+ EMAIL_BODY = "Python rules them all!" # user defined variable, in Zabbix is {ALERT.MESSAGE}
6365
6466argc = len (sys .argv )
6567if not (argc == 1 or argc == 4 ):
@@ -70,19 +72,32 @@ def usage():
7072else :
7173 if sys .argv [1 ] != '' and sys .argv [2 ] != '' and sys .argv [3 ] != '' :
7274 EMAIL_TO = sys .argv [1 ]
73- SUBJECT = sys .argv [2 ]
74- text = sys .argv [3 ]
75+ EMAIL_SUBJECT = sys .argv [2 ]
76+ EMAIL_BODY = sys .argv [3 ]
7577
76- BODY = string .join ((
77- "From: %s" % DEFAULT_FROM_EMAIL ,
78- "To: %s" % EMAIL_TO ,
79- "Subject: %s" % SUBJECT ,
80- "" ,
81- text
82- ), CRLF )
78+ if six .PY2 :
79+ BODY = string .join ((
80+ "From: %s" % DEFAULT_FROM_EMAIL ,
81+ "To: %s" % EMAIL_TO ,
82+ "Subject: %s" % EMAIL_SUBJECT ,
83+ "" ,
84+ EMAIL_BODY
85+ ), CRLF )
86+ else :
87+ BODY = CRLF .join ((
88+ "From: %s" % DEFAULT_FROM_EMAIL ,
89+ "To: %s" % EMAIL_TO ,
90+ "Subject: %s" % EMAIL_SUBJECT ,
91+ "" ,
92+ EMAIL_BODY
93+ ))
8394
84- if EMAIL_HOST_ENABLE_SSL :
85- server = smtplib .SMTP_SSL ()
95+ if EMAIL_HOST_ENABLE_SSL or EMAIL_HOST_ENABLE_TLS :
96+ if six .PY3 :
97+ # for python3, ValueError: server_hostname cannot be an empty string or start with a leading dot.
98+ server = smtplib .SMTP_SSL (host = EMAIL_HOST )
99+ else :
100+ server = smtplib .SMTP_SSL ()
86101else :
87102 server = smtplib .SMTP ()
88103
0 commit comments