import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
import subprocess
import time
zabbix_server = ‘192.168.1.100’
zabbix_user = ‘admin’
zabbix_password = ‘zabbix’
zabbix_export_script = ‘/usr/lib/zabbix/export_zabbix_data.py’
zabbix_export_output = ‘/tmp/zabbix_data.csv’
smtp_server = ‘smtp.example.com’
smtp_port = 587
smtp_username = ‘sender@example.com’
smtp_password = ‘password’
email_from = ‘sender@example.com’
email_to = [‘recipient@example.com’]
email_subject = ‘Zabbix Data Export’
email_body = ‘Please see the attached Zabbix data file.’
subprocess.call([‘python’, zabbix_export_script, zabbix_server, zabbix_user, zabbix_password, zabbix_export_output])
msg = MIMEMultipart()
msg[‘From’] = email_from
msg[‘To’] = ‘,’.join(email_to)
msg[‘Subject’] = email_subject
msg.attach(MIMEText(email_body, ‘plain’))
attachment = open(zabbix_export_output, ‘rb’)
part = MIMEBase(‘application’, ‘octet-stream’)
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header(‘Content-Disposition’, “attachment; filename= %s” % os.path.basename(zabbix_export_output))
msg.attach(part)
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(smtp_username, smtp_password)
server.sendmail(email_from, email_to, msg.as_string())
server.quit()
print(“Email sent successfully”)